forked from xiph/daala
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_code.c
More file actions
156 lines (143 loc) · 5.52 KB
/
Copy pathgeneric_code.c
File metadata and controls
156 lines (143 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*Daala video codec
Copyright (c) 2012 Daala project contributors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "generic_code.h"
void od_cdf_init(uint16_t *cdf, int ncdfs, int nsyms, int val, int first) {
int i;
int j;
for (i = 0; i < ncdfs; i++) {
for (j = 0; j < nsyms; j++) {
cdf[i*nsyms + j] = val*j + first;
}
}
}
/** Adapts a Q15 cdf after encoding/decoding a symbol. */
void od_cdf_adapt_q15(int val, uint16_t *cdf, int n, int *count, int rate) {
int i;
*count = OD_MINI(*count + 1, 1 << rate);
OD_ASSERT(cdf[n - 1] == 32768);
if (*count >= 1 << rate) {
/* Steady-state adaptation based on a simple IIR with dyadic rate. */
for (i = 0; i < n; i++) {
int tmp;
/* When (i < val), we want the adjustment ((cdf[i] - tmp) >> rate) to be
positive so long as (cdf[i] > i + 1), and 0 when (cdf[i] == i + 1),
to ensure we don't drive any probabilities to 0. Replacing cdf[i] with
(i + 2) and solving ((i + 2 - tmp) >> rate == 1) for tmp produces
tmp == i + 2 - (1 << rate). Using this value of tmp with
cdf[i] == i + 1 instead gives an adjustment of 0 as desired.
When (i >= val), we want ((cdf[i] - tmp) >> rate) to be negative so
long as cdf[i] < 32768 - (n - 1 - i), and 0 when
cdf[i] == 32768 - (n - 1 - i), again to ensure we don't drive any
probabilities to 0. Since right-shifting any negative value is still
negative, we can solve (32768 - (n - 1 - i) - tmp == 0) for tmp,
producing tmp = 32769 - n + i. Using this value of tmp with smaller
values of cdf[i] instead gives negative adjustments, as desired.
Combining the two cases gives the expression below. These could be
stored in a lookup table indexed by n and rate to avoid the
arithmetic. */
tmp = 2 - (1<<rate) + i + (32767 + (1<<rate) - n)*(i >= val);
cdf[i] -= (cdf[i] - tmp) >> rate;
}
}
else {
int alpha;
/* Initial adaptation for the first symbols. The adaptation rate is
computed to be equivalent to what od_{en,de}code_cdf_adapt() does
when the initial cdf is set to increment/4. */
alpha = 4*32768/(n + 4**count);
for (i = 0; i < n; i++) {
int tmp;
tmp = (32768 - n)*(i >= val) + i + 1;
cdf[i] -= ((cdf[i] - tmp)*alpha) >> 15;
}
}
OD_ASSERT(cdf[n - 1] == 32768);
}
/** Initializes the cdfs and freq counts for a model.
*
* @param [out] model model being initialized
*/
void generic_model_init(generic_encoder *model) {
int i;
int j;
model->increment = 64;
for (i = 0; i < GENERIC_TABLES; i++) {
for (j = 0; j < 16; j++) {
/* Do flat initialization equivalent to a single symbol in each bin. */
model->cdf[i][j] = (j + 1) * model->increment;
}
}
}
/** Takes the base-2 log of E(x) in Q1.
*
* @param [in] ExQ16 expectation of x in Q16
*
* @retval 2*log2(ExQ16/2^16)
*/
int log_ex(int ex_q16) {
int lg;
int lg_q1;
int odd;
lg = OD_ILOG(ex_q16);
if (lg < 15) {
odd = ex_q16*ex_q16 > 2 << 2*lg;
}
else {
int tmp;
tmp = ex_q16 >> (lg - 8);
odd = tmp*tmp > (1 << 15);
}
lg_q1 = OD_MAXI(0, 2*lg - 33 + odd);
return lg_q1;
}
/** Updates the probability model based on the encoded/decoded value
*
* @param [in,out] model generic prob model
* @param [in,out] ExQ16 expectation of x
* @param [in] x variable encoded/decoded (used for ExQ16)
* @param [in] xs variable x after shift (used for the model)
* @param [in] id id of the icdf to adapt
* @param [in] integration integration period of ExQ16 (leaky average over
* 1<<integration samples)
*/
void generic_model_update(generic_encoder *model, int *ex_q16, int x, int xs,
int id, int integration) {
int i;
int xenc;
uint16_t *cdf;
cdf = model->cdf[id];
/* Renormalize if we cannot add increment */
if (cdf[15] + model->increment > 32767) {
for (i = 0; i < 16; i++) {
/* Second term ensures that the pdf is non-null */
cdf[i] = (cdf[i] >> 1) + i + 1;
}
}
/* Update freq count */
xenc = OD_MINI(15, xs);
/* This can be easily vectorized */
for (i = xenc; i < 16; i++) cdf[i] += model->increment;
/* We could have saturated ExQ16 directly, but this is safe and simpler */
x = OD_MINI(x, 32767);
OD_IIR_DIADIC(*ex_q16, x << 16, integration);
}