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