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 :
12 : #ifndef AOM_DSP_PROB_H_
13 : #define AOM_DSP_PROB_H_
14 :
15 : #include <assert.h>
16 :
17 : #include "./aom_config.h"
18 : #include "./aom_dsp_common.h"
19 :
20 : #include "aom_ports/bitops.h"
21 : #include "aom_ports/mem.h"
22 :
23 : #if !CONFIG_ANS
24 : #include "aom_dsp/entcode.h"
25 : #endif
26 :
27 : #ifdef __cplusplus
28 : extern "C" {
29 : #endif
30 :
31 : typedef uint8_t aom_prob;
32 :
33 : // TODO(negge): Rename this aom_prob once we remove vpxbool.
34 : typedef uint16_t aom_cdf_prob;
35 :
36 : #define CDF_SIZE(x) ((x) + 1)
37 :
38 : #define CDF_PROB_BITS 15
39 : #define CDF_PROB_TOP (1 << CDF_PROB_BITS)
40 :
41 : #if !CONFIG_ANS
42 : #define AOM_ICDF OD_ICDF
43 : #else
44 : #define AOM_ICDF(x) (x)
45 : #endif
46 :
47 : #define MAX_PROB 255
48 :
49 : #define aom_prob_half ((aom_prob)128)
50 :
51 : typedef int8_t aom_tree_index;
52 :
53 : #define TREE_SIZE(leaf_count) (-2 + 2 * (leaf_count))
54 :
55 : #define MODE_MV_COUNT_SAT 20
56 :
57 : /* We build coding trees compactly in arrays.
58 : Each node of the tree is a pair of aom_tree_indices.
59 : Array index often references a corresponding probability table.
60 : Index <= 0 means done encoding/decoding and value = -Index,
61 : Index > 0 means need another bit, specification at index.
62 : Nonnegative indices are always even; processing begins at node 0. */
63 :
64 : typedef const aom_tree_index aom_tree[];
65 :
66 0 : static INLINE aom_prob get_prob(unsigned int num, unsigned int den) {
67 0 : assert(den != 0);
68 : {
69 0 : const int p = (int)(((uint64_t)num * 256 + (den >> 1)) / den);
70 : // (p > 255) ? 255 : (p < 1) ? 1 : p;
71 0 : const int clipped_prob = p | ((255 - p) >> 23) | (p == 0);
72 0 : return (aom_prob)clipped_prob;
73 : }
74 : }
75 :
76 0 : static INLINE aom_prob get_binary_prob(unsigned int n0, unsigned int n1) {
77 0 : const unsigned int den = n0 + n1;
78 0 : if (den == 0) return 128u;
79 0 : return get_prob(n0, den);
80 : }
81 :
82 : /* This function assumes prob1 and prob2 are already within [1,255] range. */
83 0 : static INLINE aom_prob weighted_prob(int prob1, int prob2, int factor) {
84 0 : return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8);
85 : }
86 :
87 0 : static INLINE aom_prob merge_probs(aom_prob pre_prob, const unsigned int ct[2],
88 : unsigned int count_sat,
89 : unsigned int max_update_factor) {
90 0 : const aom_prob prob = get_binary_prob(ct[0], ct[1]);
91 0 : const unsigned int count = AOMMIN(ct[0] + ct[1], count_sat);
92 0 : const unsigned int factor = max_update_factor * count / count_sat;
93 0 : return weighted_prob(pre_prob, prob, factor);
94 : }
95 :
96 : // MODE_MV_MAX_UPDATE_FACTOR (128) * count / MODE_MV_COUNT_SAT;
97 : static const int count_to_update_factor[MODE_MV_COUNT_SAT + 1] = {
98 : 0, 6, 12, 19, 25, 32, 38, 44, 51, 57, 64,
99 : 70, 76, 83, 89, 96, 102, 108, 115, 121, 128
100 : };
101 :
102 0 : static INLINE aom_prob mode_mv_merge_probs(aom_prob pre_prob,
103 : const unsigned int ct[2]) {
104 0 : const unsigned int den = ct[0] + ct[1];
105 0 : if (den == 0) {
106 0 : return pre_prob;
107 : } else {
108 0 : const unsigned int count = AOMMIN(den, MODE_MV_COUNT_SAT);
109 0 : const unsigned int factor = count_to_update_factor[count];
110 0 : const aom_prob prob = get_prob(ct[0], den);
111 0 : return weighted_prob(pre_prob, prob, factor);
112 : }
113 : }
114 :
115 : void aom_tree_merge_probs(const aom_tree_index *tree, const aom_prob *pre_probs,
116 : const unsigned int *counts, aom_prob *probs);
117 :
118 : int tree_to_cdf(const aom_tree_index *tree, const aom_prob *probs,
119 : aom_tree_index root, aom_cdf_prob *cdf, aom_tree_index *ind,
120 : int *pth, int *len);
121 :
122 : static INLINE void av1_tree_to_cdf(const aom_tree_index *tree,
123 : const aom_prob *probs, aom_cdf_prob *cdf) {
124 : aom_tree_index index[16];
125 : int path[16];
126 : int dist[16];
127 : tree_to_cdf(tree, probs, 0, cdf, index, path, dist);
128 : }
129 :
130 : #define av1_tree_to_cdf_1D(tree, probs, cdf, u) \
131 : do { \
132 : int i; \
133 : for (i = 0; i < u; i++) { \
134 : av1_tree_to_cdf(tree, probs[i], cdf[i]); \
135 : } \
136 : } while (0)
137 :
138 : #define av1_tree_to_cdf_2D(tree, probs, cdf, v, u) \
139 : do { \
140 : int j; \
141 : int i; \
142 : for (j = 0; j < v; j++) { \
143 : for (i = 0; i < u; i++) { \
144 : av1_tree_to_cdf(tree, probs[j][i], cdf[j][i]); \
145 : } \
146 : } \
147 : } while (0)
148 :
149 : void av1_indices_from_tree(int *ind, int *inv, const aom_tree_index *tree);
150 :
151 : #if CONFIG_EC_ADAPT
152 0 : static INLINE void update_cdf(aom_cdf_prob *cdf, int val, int nsymbs) {
153 0 : const int rate = 4 + (cdf[nsymbs] > 31) + get_msb(nsymbs);
154 0 : const int rate2 = 5;
155 : int i, tmp;
156 : int diff;
157 : #if 1
158 0 : const int tmp0 = 1 << rate2;
159 0 : tmp = AOM_ICDF(tmp0);
160 0 : diff = ((CDF_PROB_TOP - (nsymbs << rate2)) >> rate) << rate;
161 : // Single loop (faster)
162 : #if !CONFIG_ANS && CONFIG_EC_SMALLMUL
163 0 : for (i = 0; i < nsymbs - 1; ++i, tmp -= tmp0) {
164 0 : tmp -= (i == val ? diff : 0);
165 0 : cdf[i] += ((tmp - cdf[i]) >> rate);
166 : }
167 : #else
168 : for (i = 0; i < nsymbs - 1; ++i, tmp += tmp0) {
169 : tmp += (i == val ? diff : 0);
170 : cdf[i] -= ((cdf[i] - tmp) >> rate);
171 : }
172 : #endif
173 : #else
174 : for (i = 0; i < nsymbs; ++i) {
175 : tmp = (i + 1) << rate2;
176 : cdf[i] -= ((cdf[i] - tmp) >> rate);
177 : }
178 : diff = CDF_PROB_TOP - cdf[nsymbs - 1];
179 :
180 : for (i = val; i < nsymbs; ++i) {
181 : cdf[i] += diff;
182 : }
183 : #endif
184 0 : cdf[nsymbs] += (cdf[nsymbs] < 32);
185 0 : }
186 : #endif
187 :
188 : #ifdef __cplusplus
189 : } // extern "C"
190 : #endif
191 :
192 : #endif // AOM_DSP_PROB_H_
|