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
12 #ifndef AOM_AOM_DSP_PROB_H_
13 #define AOM_AOM_DSP_PROB_H_
14
15 #include <assert.h>
16 #include <stdio.h>
17
18 #include "config/aom_config.h"
19
20 #include "aom_dsp/aom_dsp_common.h"
21 #include "aom_dsp/entcode.h"
22 #include "aom_ports/bitops.h"
23 #include "aom_ports/mem.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 // TODO(negge): Rename this aom_prob once we remove vpxbool.
30 typedef uint16_t aom_cdf_prob;
31
32 #define CDF_SIZE(x) ((x) + 1)
33 #define CDF_PROB_BITS 15
34 #define CDF_PROB_TOP (1 << CDF_PROB_BITS)
35 #define CDF_INIT_TOP 32768
36 #define CDF_SHIFT (15 - CDF_PROB_BITS)
37 /*The value stored in an iCDF is CDF_PROB_TOP minus the actual cumulative
38 probability (an "inverse" CDF).
39 This function converts from one representation to the other (and is its own
40 inverse).*/
41 #define AOM_ICDF(x) (CDF_PROB_TOP - (x))
42
43 #if CDF_SHIFT == 0
44
45 #define AOM_CDF2(a0) AOM_ICDF(a0), AOM_ICDF(CDF_PROB_TOP), 0
46 #define AOM_CDF3(a0, a1) AOM_ICDF(a0), AOM_ICDF(a1), AOM_ICDF(CDF_PROB_TOP), 0
47 #define AOM_CDF4(a0, a1, a2) \
48 AOM_ICDF(a0), AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(CDF_PROB_TOP), 0
49 #define AOM_CDF5(a0, a1, a2, a3) \
50 AOM_ICDF(a0) \
51 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(CDF_PROB_TOP), 0
52 #define AOM_CDF6(a0, a1, a2, a3, a4) \
53 AOM_ICDF(a0) \
54 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), \
55 AOM_ICDF(CDF_PROB_TOP), 0
56 #define AOM_CDF7(a0, a1, a2, a3, a4, a5) \
57 AOM_ICDF(a0) \
58 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
59 AOM_ICDF(CDF_PROB_TOP), 0
60 #define AOM_CDF8(a0, a1, a2, a3, a4, a5, a6) \
61 AOM_ICDF(a0) \
62 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
63 AOM_ICDF(a6), AOM_ICDF(CDF_PROB_TOP), 0
64 #define AOM_CDF9(a0, a1, a2, a3, a4, a5, a6, a7) \
65 AOM_ICDF(a0) \
66 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
67 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(CDF_PROB_TOP), 0
68 #define AOM_CDF10(a0, a1, a2, a3, a4, a5, a6, a7, a8) \
69 AOM_ICDF(a0) \
70 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
71 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(CDF_PROB_TOP), 0
72 #define AOM_CDF11(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
73 AOM_ICDF(a0) \
74 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
75 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(a9), \
76 AOM_ICDF(CDF_PROB_TOP), 0
77 #define AOM_CDF12(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) \
78 AOM_ICDF(a0) \
79 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
80 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(a9), AOM_ICDF(a10), \
81 AOM_ICDF(CDF_PROB_TOP), 0
82 #define AOM_CDF13(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) \
83 AOM_ICDF(a0) \
84 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
85 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(a9), AOM_ICDF(a10), \
86 AOM_ICDF(a11), AOM_ICDF(CDF_PROB_TOP), 0
87 #define AOM_CDF14(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) \
88 AOM_ICDF(a0) \
89 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
90 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(a9), AOM_ICDF(a10), \
91 AOM_ICDF(a11), AOM_ICDF(a12), AOM_ICDF(CDF_PROB_TOP), 0
92 #define AOM_CDF15(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) \
93 AOM_ICDF(a0) \
94 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
95 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(a9), AOM_ICDF(a10), \
96 AOM_ICDF(a11), AOM_ICDF(a12), AOM_ICDF(a13), AOM_ICDF(CDF_PROB_TOP), 0
97 #define AOM_CDF16(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, \
98 a14) \
99 AOM_ICDF(a0) \
100 , AOM_ICDF(a1), AOM_ICDF(a2), AOM_ICDF(a3), AOM_ICDF(a4), AOM_ICDF(a5), \
101 AOM_ICDF(a6), AOM_ICDF(a7), AOM_ICDF(a8), AOM_ICDF(a9), AOM_ICDF(a10), \
102 AOM_ICDF(a11), AOM_ICDF(a12), AOM_ICDF(a13), AOM_ICDF(a14), \
103 AOM_ICDF(CDF_PROB_TOP), 0
104
105 #else
106 #define AOM_CDF2(a0) \
107 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 2) + \
108 ((CDF_INIT_TOP - 2) >> 1)) / \
109 ((CDF_INIT_TOP - 2)) + \
110 1) \
111 , AOM_ICDF(CDF_PROB_TOP), 0
112 #define AOM_CDF3(a0, a1) \
113 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 3) + \
114 ((CDF_INIT_TOP - 3) >> 1)) / \
115 ((CDF_INIT_TOP - 3)) + \
116 1) \
117 , \
118 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 3) + \
119 ((CDF_INIT_TOP - 3) >> 1)) / \
120 ((CDF_INIT_TOP - 3)) + \
121 2), \
122 AOM_ICDF(CDF_PROB_TOP), 0
123 #define AOM_CDF4(a0, a1, a2) \
124 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 4) + \
125 ((CDF_INIT_TOP - 4) >> 1)) / \
126 ((CDF_INIT_TOP - 4)) + \
127 1) \
128 , \
129 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 4) + \
130 ((CDF_INIT_TOP - 4) >> 1)) / \
131 ((CDF_INIT_TOP - 4)) + \
132 2), \
133 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 4) + \
134 ((CDF_INIT_TOP - 4) >> 1)) / \
135 ((CDF_INIT_TOP - 4)) + \
136 3), \
137 AOM_ICDF(CDF_PROB_TOP), 0
138 #define AOM_CDF5(a0, a1, a2, a3) \
139 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 5) + \
140 ((CDF_INIT_TOP - 5) >> 1)) / \
141 ((CDF_INIT_TOP - 5)) + \
142 1) \
143 , \
144 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 5) + \
145 ((CDF_INIT_TOP - 5) >> 1)) / \
146 ((CDF_INIT_TOP - 5)) + \
147 2), \
148 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 5) + \
149 ((CDF_INIT_TOP - 5) >> 1)) / \
150 ((CDF_INIT_TOP - 5)) + \
151 3), \
152 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 5) + \
153 ((CDF_INIT_TOP - 5) >> 1)) / \
154 ((CDF_INIT_TOP - 5)) + \
155 4), \
156 AOM_ICDF(CDF_PROB_TOP), 0
157 #define AOM_CDF6(a0, a1, a2, a3, a4) \
158 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 6) + \
159 ((CDF_INIT_TOP - 6) >> 1)) / \
160 ((CDF_INIT_TOP - 6)) + \
161 1) \
162 , \
163 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 6) + \
164 ((CDF_INIT_TOP - 6) >> 1)) / \
165 ((CDF_INIT_TOP - 6)) + \
166 2), \
167 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 6) + \
168 ((CDF_INIT_TOP - 6) >> 1)) / \
169 ((CDF_INIT_TOP - 6)) + \
170 3), \
171 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 6) + \
172 ((CDF_INIT_TOP - 6) >> 1)) / \
173 ((CDF_INIT_TOP - 6)) + \
174 4), \
175 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 6) + \
176 ((CDF_INIT_TOP - 6) >> 1)) / \
177 ((CDF_INIT_TOP - 6)) + \
178 5), \
179 AOM_ICDF(CDF_PROB_TOP), 0
180 #define AOM_CDF7(a0, a1, a2, a3, a4, a5) \
181 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 7) + \
182 ((CDF_INIT_TOP - 7) >> 1)) / \
183 ((CDF_INIT_TOP - 7)) + \
184 1) \
185 , \
186 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 7) + \
187 ((CDF_INIT_TOP - 7) >> 1)) / \
188 ((CDF_INIT_TOP - 7)) + \
189 2), \
190 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 7) + \
191 ((CDF_INIT_TOP - 7) >> 1)) / \
192 ((CDF_INIT_TOP - 7)) + \
193 3), \
194 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 7) + \
195 ((CDF_INIT_TOP - 7) >> 1)) / \
196 ((CDF_INIT_TOP - 7)) + \
197 4), \
198 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 7) + \
199 ((CDF_INIT_TOP - 7) >> 1)) / \
200 ((CDF_INIT_TOP - 7)) + \
201 5), \
202 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 7) + \
203 ((CDF_INIT_TOP - 7) >> 1)) / \
204 ((CDF_INIT_TOP - 7)) + \
205 6), \
206 AOM_ICDF(CDF_PROB_TOP), 0
207 #define AOM_CDF8(a0, a1, a2, a3, a4, a5, a6) \
208 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
209 ((CDF_INIT_TOP - 8) >> 1)) / \
210 ((CDF_INIT_TOP - 8)) + \
211 1) \
212 , \
213 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
214 ((CDF_INIT_TOP - 8) >> 1)) / \
215 ((CDF_INIT_TOP - 8)) + \
216 2), \
217 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
218 ((CDF_INIT_TOP - 8) >> 1)) / \
219 ((CDF_INIT_TOP - 8)) + \
220 3), \
221 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
222 ((CDF_INIT_TOP - 8) >> 1)) / \
223 ((CDF_INIT_TOP - 8)) + \
224 4), \
225 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
226 ((CDF_INIT_TOP - 8) >> 1)) / \
227 ((CDF_INIT_TOP - 8)) + \
228 5), \
229 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
230 ((CDF_INIT_TOP - 8) >> 1)) / \
231 ((CDF_INIT_TOP - 8)) + \
232 6), \
233 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 8) + \
234 ((CDF_INIT_TOP - 8) >> 1)) / \
235 ((CDF_INIT_TOP - 8)) + \
236 7), \
237 AOM_ICDF(CDF_PROB_TOP), 0
238 #define AOM_CDF9(a0, a1, a2, a3, a4, a5, a6, a7) \
239 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
240 ((CDF_INIT_TOP - 9) >> 1)) / \
241 ((CDF_INIT_TOP - 9)) + \
242 1) \
243 , \
244 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
245 ((CDF_INIT_TOP - 9) >> 1)) / \
246 ((CDF_INIT_TOP - 9)) + \
247 2), \
248 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
249 ((CDF_INIT_TOP - 9) >> 1)) / \
250 ((CDF_INIT_TOP - 9)) + \
251 3), \
252 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
253 ((CDF_INIT_TOP - 9) >> 1)) / \
254 ((CDF_INIT_TOP - 9)) + \
255 4), \
256 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
257 ((CDF_INIT_TOP - 9) >> 1)) / \
258 ((CDF_INIT_TOP - 9)) + \
259 5), \
260 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
261 ((CDF_INIT_TOP - 9) >> 1)) / \
262 ((CDF_INIT_TOP - 9)) + \
263 6), \
264 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
265 ((CDF_INIT_TOP - 9) >> 1)) / \
266 ((CDF_INIT_TOP - 9)) + \
267 7), \
268 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 9) + \
269 ((CDF_INIT_TOP - 9) >> 1)) / \
270 ((CDF_INIT_TOP - 9)) + \
271 8), \
272 AOM_ICDF(CDF_PROB_TOP), 0
273 #define AOM_CDF10(a0, a1, a2, a3, a4, a5, a6, a7, a8) \
274 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
275 ((CDF_INIT_TOP - 10) >> 1)) / \
276 ((CDF_INIT_TOP - 10)) + \
277 1) \
278 , \
279 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
280 ((CDF_INIT_TOP - 10) >> 1)) / \
281 ((CDF_INIT_TOP - 10)) + \
282 2), \
283 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
284 ((CDF_INIT_TOP - 10) >> 1)) / \
285 ((CDF_INIT_TOP - 10)) + \
286 3), \
287 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
288 ((CDF_INIT_TOP - 10) >> 1)) / \
289 ((CDF_INIT_TOP - 10)) + \
290 4), \
291 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
292 ((CDF_INIT_TOP - 10) >> 1)) / \
293 ((CDF_INIT_TOP - 10)) + \
294 5), \
295 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
296 ((CDF_INIT_TOP - 10) >> 1)) / \
297 ((CDF_INIT_TOP - 10)) + \
298 6), \
299 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
300 ((CDF_INIT_TOP - 10) >> 1)) / \
301 ((CDF_INIT_TOP - 10)) + \
302 7), \
303 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
304 ((CDF_INIT_TOP - 10) >> 1)) / \
305 ((CDF_INIT_TOP - 10)) + \
306 8), \
307 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 10) + \
308 ((CDF_INIT_TOP - 10) >> 1)) / \
309 ((CDF_INIT_TOP - 10)) + \
310 9), \
311 AOM_ICDF(CDF_PROB_TOP), 0
312 #define AOM_CDF11(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
313 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
314 ((CDF_INIT_TOP - 11) >> 1)) / \
315 ((CDF_INIT_TOP - 11)) + \
316 1) \
317 , \
318 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
319 ((CDF_INIT_TOP - 11) >> 1)) / \
320 ((CDF_INIT_TOP - 11)) + \
321 2), \
322 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
323 ((CDF_INIT_TOP - 11) >> 1)) / \
324 ((CDF_INIT_TOP - 11)) + \
325 3), \
326 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
327 ((CDF_INIT_TOP - 11) >> 1)) / \
328 ((CDF_INIT_TOP - 11)) + \
329 4), \
330 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
331 ((CDF_INIT_TOP - 11) >> 1)) / \
332 ((CDF_INIT_TOP - 11)) + \
333 5), \
334 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
335 ((CDF_INIT_TOP - 11) >> 1)) / \
336 ((CDF_INIT_TOP - 11)) + \
337 6), \
338 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
339 ((CDF_INIT_TOP - 11) >> 1)) / \
340 ((CDF_INIT_TOP - 11)) + \
341 7), \
342 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
343 ((CDF_INIT_TOP - 11) >> 1)) / \
344 ((CDF_INIT_TOP - 11)) + \
345 8), \
346 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
347 ((CDF_INIT_TOP - 11) >> 1)) / \
348 ((CDF_INIT_TOP - 11)) + \
349 9), \
350 AOM_ICDF((((a9)-10) * ((CDF_INIT_TOP >> CDF_SHIFT) - 11) + \
351 ((CDF_INIT_TOP - 11) >> 1)) / \
352 ((CDF_INIT_TOP - 11)) + \
353 10), \
354 AOM_ICDF(CDF_PROB_TOP), 0
355 #define AOM_CDF12(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) \
356 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
357 ((CDF_INIT_TOP - 12) >> 1)) / \
358 ((CDF_INIT_TOP - 12)) + \
359 1) \
360 , \
361 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
362 ((CDF_INIT_TOP - 12) >> 1)) / \
363 ((CDF_INIT_TOP - 12)) + \
364 2), \
365 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
366 ((CDF_INIT_TOP - 12) >> 1)) / \
367 ((CDF_INIT_TOP - 12)) + \
368 3), \
369 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
370 ((CDF_INIT_TOP - 12) >> 1)) / \
371 ((CDF_INIT_TOP - 12)) + \
372 4), \
373 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
374 ((CDF_INIT_TOP - 12) >> 1)) / \
375 ((CDF_INIT_TOP - 12)) + \
376 5), \
377 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
378 ((CDF_INIT_TOP - 12) >> 1)) / \
379 ((CDF_INIT_TOP - 12)) + \
380 6), \
381 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
382 ((CDF_INIT_TOP - 12) >> 1)) / \
383 ((CDF_INIT_TOP - 12)) + \
384 7), \
385 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
386 ((CDF_INIT_TOP - 12) >> 1)) / \
387 ((CDF_INIT_TOP - 12)) + \
388 8), \
389 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
390 ((CDF_INIT_TOP - 12) >> 1)) / \
391 ((CDF_INIT_TOP - 12)) + \
392 9), \
393 AOM_ICDF((((a9)-10) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
394 ((CDF_INIT_TOP - 12) >> 1)) / \
395 ((CDF_INIT_TOP - 12)) + \
396 10), \
397 AOM_ICDF((((a10)-11) * ((CDF_INIT_TOP >> CDF_SHIFT) - 12) + \
398 ((CDF_INIT_TOP - 12) >> 1)) / \
399 ((CDF_INIT_TOP - 12)) + \
400 11), \
401 AOM_ICDF(CDF_PROB_TOP), 0
402 #define AOM_CDF13(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) \
403 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
404 ((CDF_INIT_TOP - 13) >> 1)) / \
405 ((CDF_INIT_TOP - 13)) + \
406 1) \
407 , \
408 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
409 ((CDF_INIT_TOP - 13) >> 1)) / \
410 ((CDF_INIT_TOP - 13)) + \
411 2), \
412 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
413 ((CDF_INIT_TOP - 13) >> 1)) / \
414 ((CDF_INIT_TOP - 13)) + \
415 3), \
416 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
417 ((CDF_INIT_TOP - 13) >> 1)) / \
418 ((CDF_INIT_TOP - 13)) + \
419 4), \
420 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
421 ((CDF_INIT_TOP - 13) >> 1)) / \
422 ((CDF_INIT_TOP - 13)) + \
423 5), \
424 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
425 ((CDF_INIT_TOP - 13) >> 1)) / \
426 ((CDF_INIT_TOP - 13)) + \
427 6), \
428 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
429 ((CDF_INIT_TOP - 13) >> 1)) / \
430 ((CDF_INIT_TOP - 13)) + \
431 7), \
432 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
433 ((CDF_INIT_TOP - 13) >> 1)) / \
434 ((CDF_INIT_TOP - 13)) + \
435 8), \
436 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
437 ((CDF_INIT_TOP - 13) >> 1)) / \
438 ((CDF_INIT_TOP - 13)) + \
439 9), \
440 AOM_ICDF((((a9)-10) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
441 ((CDF_INIT_TOP - 13) >> 1)) / \
442 ((CDF_INIT_TOP - 13)) + \
443 10), \
444 AOM_ICDF((((a10)-11) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
445 ((CDF_INIT_TOP - 13) >> 1)) / \
446 ((CDF_INIT_TOP - 13)) + \
447 11), \
448 AOM_ICDF((((a11)-12) * ((CDF_INIT_TOP >> CDF_SHIFT) - 13) + \
449 ((CDF_INIT_TOP - 13) >> 1)) / \
450 ((CDF_INIT_TOP - 13)) + \
451 12), \
452 AOM_ICDF(CDF_PROB_TOP), 0
453 #define AOM_CDF14(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) \
454 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
455 ((CDF_INIT_TOP - 14) >> 1)) / \
456 ((CDF_INIT_TOP - 14)) + \
457 1) \
458 , \
459 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
460 ((CDF_INIT_TOP - 14) >> 1)) / \
461 ((CDF_INIT_TOP - 14)) + \
462 2), \
463 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
464 ((CDF_INIT_TOP - 14) >> 1)) / \
465 ((CDF_INIT_TOP - 14)) + \
466 3), \
467 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
468 ((CDF_INIT_TOP - 14) >> 1)) / \
469 ((CDF_INIT_TOP - 14)) + \
470 4), \
471 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
472 ((CDF_INIT_TOP - 14) >> 1)) / \
473 ((CDF_INIT_TOP - 14)) + \
474 5), \
475 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
476 ((CDF_INIT_TOP - 14) >> 1)) / \
477 ((CDF_INIT_TOP - 14)) + \
478 6), \
479 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
480 ((CDF_INIT_TOP - 14) >> 1)) / \
481 ((CDF_INIT_TOP - 14)) + \
482 7), \
483 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
484 ((CDF_INIT_TOP - 14) >> 1)) / \
485 ((CDF_INIT_TOP - 14)) + \
486 8), \
487 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
488 ((CDF_INIT_TOP - 14) >> 1)) / \
489 ((CDF_INIT_TOP - 14)) + \
490 9), \
491 AOM_ICDF((((a9)-10) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
492 ((CDF_INIT_TOP - 14) >> 1)) / \
493 ((CDF_INIT_TOP - 14)) + \
494 10), \
495 AOM_ICDF((((a10)-11) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
496 ((CDF_INIT_TOP - 14) >> 1)) / \
497 ((CDF_INIT_TOP - 14)) + \
498 11), \
499 AOM_ICDF((((a11)-12) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
500 ((CDF_INIT_TOP - 14) >> 1)) / \
501 ((CDF_INIT_TOP - 14)) + \
502 12), \
503 AOM_ICDF((((a12)-13) * ((CDF_INIT_TOP >> CDF_SHIFT) - 14) + \
504 ((CDF_INIT_TOP - 14) >> 1)) / \
505 ((CDF_INIT_TOP - 14)) + \
506 13), \
507 AOM_ICDF(CDF_PROB_TOP), 0
508 #define AOM_CDF15(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) \
509 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
510 ((CDF_INIT_TOP - 15) >> 1)) / \
511 ((CDF_INIT_TOP - 15)) + \
512 1) \
513 , \
514 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
515 ((CDF_INIT_TOP - 15) >> 1)) / \
516 ((CDF_INIT_TOP - 15)) + \
517 2), \
518 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
519 ((CDF_INIT_TOP - 15) >> 1)) / \
520 ((CDF_INIT_TOP - 15)) + \
521 3), \
522 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
523 ((CDF_INIT_TOP - 15) >> 1)) / \
524 ((CDF_INIT_TOP - 15)) + \
525 4), \
526 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
527 ((CDF_INIT_TOP - 15) >> 1)) / \
528 ((CDF_INIT_TOP - 15)) + \
529 5), \
530 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
531 ((CDF_INIT_TOP - 15) >> 1)) / \
532 ((CDF_INIT_TOP - 15)) + \
533 6), \
534 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
535 ((CDF_INIT_TOP - 15) >> 1)) / \
536 ((CDF_INIT_TOP - 15)) + \
537 7), \
538 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
539 ((CDF_INIT_TOP - 15) >> 1)) / \
540 ((CDF_INIT_TOP - 15)) + \
541 8), \
542 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
543 ((CDF_INIT_TOP - 15) >> 1)) / \
544 ((CDF_INIT_TOP - 15)) + \
545 9), \
546 AOM_ICDF((((a9)-10) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
547 ((CDF_INIT_TOP - 15) >> 1)) / \
548 ((CDF_INIT_TOP - 15)) + \
549 10), \
550 AOM_ICDF((((a10)-11) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
551 ((CDF_INIT_TOP - 15) >> 1)) / \
552 ((CDF_INIT_TOP - 15)) + \
553 11), \
554 AOM_ICDF((((a11)-12) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
555 ((CDF_INIT_TOP - 15) >> 1)) / \
556 ((CDF_INIT_TOP - 15)) + \
557 12), \
558 AOM_ICDF((((a12)-13) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
559 ((CDF_INIT_TOP - 15) >> 1)) / \
560 ((CDF_INIT_TOP - 15)) + \
561 13), \
562 AOM_ICDF((((a13)-14) * ((CDF_INIT_TOP >> CDF_SHIFT) - 15) + \
563 ((CDF_INIT_TOP - 15) >> 1)) / \
564 ((CDF_INIT_TOP - 15)) + \
565 14), \
566 AOM_ICDF(CDF_PROB_TOP), 0
567 #define AOM_CDF16(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, \
568 a14) \
569 AOM_ICDF((((a0)-1) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
570 ((CDF_INIT_TOP - 16) >> 1)) / \
571 ((CDF_INIT_TOP - 16)) + \
572 1) \
573 , \
574 AOM_ICDF((((a1)-2) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
575 ((CDF_INIT_TOP - 16) >> 1)) / \
576 ((CDF_INIT_TOP - 16)) + \
577 2), \
578 AOM_ICDF((((a2)-3) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
579 ((CDF_INIT_TOP - 16) >> 1)) / \
580 ((CDF_INIT_TOP - 16)) + \
581 3), \
582 AOM_ICDF((((a3)-4) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
583 ((CDF_INIT_TOP - 16) >> 1)) / \
584 ((CDF_INIT_TOP - 16)) + \
585 4), \
586 AOM_ICDF((((a4)-5) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
587 ((CDF_INIT_TOP - 16) >> 1)) / \
588 ((CDF_INIT_TOP - 16)) + \
589 5), \
590 AOM_ICDF((((a5)-6) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
591 ((CDF_INIT_TOP - 16) >> 1)) / \
592 ((CDF_INIT_TOP - 16)) + \
593 6), \
594 AOM_ICDF((((a6)-7) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
595 ((CDF_INIT_TOP - 16) >> 1)) / \
596 ((CDF_INIT_TOP - 16)) + \
597 7), \
598 AOM_ICDF((((a7)-8) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
599 ((CDF_INIT_TOP - 16) >> 1)) / \
600 ((CDF_INIT_TOP - 16)) + \
601 8), \
602 AOM_ICDF((((a8)-9) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
603 ((CDF_INIT_TOP - 16) >> 1)) / \
604 ((CDF_INIT_TOP - 16)) + \
605 9), \
606 AOM_ICDF((((a9)-10) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
607 ((CDF_INIT_TOP - 16) >> 1)) / \
608 ((CDF_INIT_TOP - 16)) + \
609 10), \
610 AOM_ICDF((((a10)-11) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
611 ((CDF_INIT_TOP - 16) >> 1)) / \
612 ((CDF_INIT_TOP - 16)) + \
613 11), \
614 AOM_ICDF((((a11)-12) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
615 ((CDF_INIT_TOP - 16) >> 1)) / \
616 ((CDF_INIT_TOP - 16)) + \
617 12), \
618 AOM_ICDF((((a12)-13) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
619 ((CDF_INIT_TOP - 16) >> 1)) / \
620 ((CDF_INIT_TOP - 16)) + \
621 13), \
622 AOM_ICDF((((a13)-14) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
623 ((CDF_INIT_TOP - 16) >> 1)) / \
624 ((CDF_INIT_TOP - 16)) + \
625 14), \
626 AOM_ICDF((((a14)-15) * ((CDF_INIT_TOP >> CDF_SHIFT) - 16) + \
627 ((CDF_INIT_TOP - 16) >> 1)) / \
628 ((CDF_INIT_TOP - 16)) + \
629 15), \
630 AOM_ICDF(CDF_PROB_TOP), 0
631
632 #endif
633
get_prob(unsigned int num,unsigned int den)634 static INLINE uint8_t get_prob(unsigned int num, unsigned int den) {
635 assert(den != 0);
636 {
637 const int p = (int)(((uint64_t)num * 256 + (den >> 1)) / den);
638 // (p > 255) ? 255 : (p < 1) ? 1 : p;
639 const int clipped_prob = p | ((255 - p) >> 23) | (p == 0);
640 return (uint8_t)clipped_prob;
641 }
642 }
643
update_cdf(aom_cdf_prob * cdf,int8_t val,int nsymbs)644 static INLINE void update_cdf(aom_cdf_prob *cdf, int8_t val, int nsymbs) {
645 int rate;
646 int i, tmp;
647
648 static const int nsymbs2speed[17] = { 0, 0, 1, 1, 2, 2, 2, 2, 2,
649 2, 2, 2, 2, 2, 2, 2, 2 };
650 assert(nsymbs < 17);
651 rate = 3 + (cdf[nsymbs] > 15) + (cdf[nsymbs] > 31) +
652 nsymbs2speed[nsymbs]; // + get_msb(nsymbs);
653 tmp = AOM_ICDF(0);
654
655 // Single loop (faster)
656 for (i = 0; i < nsymbs - 1; ++i) {
657 tmp = (i == val) ? 0 : tmp;
658 if (tmp < cdf[i]) {
659 cdf[i] -= ((cdf[i] - tmp) >> rate);
660 } else {
661 cdf[i] += ((tmp - cdf[i]) >> rate);
662 }
663 }
664 cdf[nsymbs] += (cdf[nsymbs] < 32);
665 }
666
667 #ifdef __cplusplus
668 } // extern "C"
669 #endif
670
671 #endif // AOM_AOM_DSP_PROB_H_
672