Coverage Report

Created: 2025-08-29 07:57

/src/opus/celt/laplace.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2007 CSIRO
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
3
   Written by Jean-Marc Valin */
4
/*
5
   Redistribution and use in source and binary forms, with or without
6
   modification, are permitted provided that the following conditions
7
   are met:
8
9
   - Redistributions of source code must retain the above copyright
10
   notice, this list of conditions and the following disclaimer.
11
12
   - Redistributions in binary form must reproduce the above copyright
13
   notice, this list of conditions and the following disclaimer in the
14
   documentation and/or other materials provided with the distribution.
15
16
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#ifdef HAVE_CONFIG_H
30
#include "config.h"
31
#endif
32
33
#include "laplace.h"
34
#include "mathops.h"
35
36
/* The minimum probability of an energy delta (out of 32768). */
37
166M
#define LAPLACE_LOG_MINP (0)
38
161M
#define LAPLACE_MINP (1<<LAPLACE_LOG_MINP)
39
/* The minimum number of guaranteed representable energy deltas (in one
40
    direction). */
41
45.8M
#define LAPLACE_NMIN (16)
42
43
/* When called, decay is positive and at most 11456. */
44
static unsigned ec_laplace_get_freq1(unsigned fs0, int decay)
45
45.8M
{
46
45.8M
   unsigned ft;
47
45.8M
   ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN) - fs0;
48
45.8M
   return ft*(opus_int32)(16384-decay)>>15;
49
45.8M
}
50
51
void ec_laplace_encode(ec_enc *enc, int *value, unsigned fs, int decay)
52
66.3M
{
53
66.3M
   unsigned fl;
54
66.3M
   int val = *value;
55
66.3M
   fl = 0;
56
66.3M
   if (val)
57
44.6M
   {
58
44.6M
      int s;
59
44.6M
      int i;
60
44.6M
      s = -(val<0);
61
44.6M
      val = (val+s)^s;
62
44.6M
      fl = fs;
63
44.6M
      fs = ec_laplace_get_freq1(fs, decay);
64
      /* Search the decaying part of the PDF.*/
65
101M
      for (i=1; fs > 0 && i < val; i++)
66
56.5M
      {
67
56.5M
         fs *= 2;
68
56.5M
         fl += fs+2*LAPLACE_MINP;
69
56.5M
         fs = (fs*(opus_int32)decay)>>15;
70
56.5M
      }
71
      /* Everything beyond that has probability LAPLACE_MINP. */
72
44.6M
      if (!fs)
73
6.76M
      {
74
6.76M
         int di;
75
6.76M
         int ndi_max;
76
6.76M
         ndi_max = (32768-fl+LAPLACE_MINP-1)>>LAPLACE_LOG_MINP;
77
6.76M
         ndi_max = (ndi_max-s)>>1;
78
6.76M
         di = IMIN(val - i, ndi_max - 1);
79
6.76M
         fl += (2*di+1+s)*LAPLACE_MINP;
80
6.76M
         fs = IMIN(LAPLACE_MINP, 32768-fl);
81
6.76M
         *value = (i+di+s)^s;
82
6.76M
      }
83
37.8M
      else
84
37.8M
      {
85
37.8M
         fs += LAPLACE_MINP;
86
37.8M
         fl += fs&~s;
87
37.8M
      }
88
44.6M
      celt_assert(fl+fs<=32768);
89
44.6M
      celt_assert(fs>0);
90
44.6M
   }
91
66.3M
   ec_encode_bin(enc, fl, fl+fs, 15);
92
66.3M
}
93
94
int ec_laplace_decode(ec_dec *dec, unsigned fs, int decay)
95
2.78M
{
96
2.78M
   int val=0;
97
2.78M
   unsigned fl;
98
2.78M
   unsigned fm;
99
2.78M
   fm = ec_decode_bin(dec, 15);
100
2.78M
   fl = 0;
101
2.78M
   if (fm >= fs)
102
1.22M
   {
103
1.22M
      val++;
104
1.22M
      fl = fs;
105
1.22M
      fs = ec_laplace_get_freq1(fs, decay)+LAPLACE_MINP;
106
      /* Search the decaying part of the PDF.*/
107
1.85M
      while(fs > LAPLACE_MINP && fm >= fl+2*fs)
108
628k
      {
109
628k
         fs *= 2;
110
628k
         fl += fs;
111
628k
         fs = ((fs-2*LAPLACE_MINP)*(opus_int32)decay)>>15;
112
628k
         fs += LAPLACE_MINP;
113
628k
         val++;
114
628k
      }
115
      /* Everything beyond that has probability LAPLACE_MINP. */
116
1.22M
      if (fs <= LAPLACE_MINP)
117
14.5k
      {
118
14.5k
         int di;
119
14.5k
         di = (fm-fl)>>(LAPLACE_LOG_MINP+1);
120
14.5k
         val += di;
121
14.5k
         fl += 2*di*LAPLACE_MINP;
122
14.5k
      }
123
1.22M
      if (fm < fl+fs)
124
612k
         val = -val;
125
613k
      else
126
613k
         fl += fs;
127
1.22M
   }
128
2.78M
   celt_assert(fl<32768);
129
2.78M
   celt_assert(fs>0);
130
2.78M
   celt_assert(fl<=fm);
131
2.78M
   celt_assert(fm<IMIN(fl+fs,32768));
132
2.78M
   ec_dec_update(dec, fl, IMIN(fl+fs,32768), 32768);
133
2.78M
   return val;
134
2.78M
}
135
136
void ec_laplace_encode_p0(ec_enc *enc, int value, opus_uint16 p0, opus_uint16 decay)
137
0
{
138
0
   int s;
139
0
   opus_uint16 sign_icdf[3];
140
0
   sign_icdf[0] = 32768-p0;
141
0
   sign_icdf[1] = sign_icdf[0]/2;
142
0
   sign_icdf[2] = 0;
143
0
   s = value == 0 ? 0 : (value > 0 ? 1 : 2);
144
0
   ec_enc_icdf16(enc, s, sign_icdf, 15);
145
0
   value = abs(value);
146
0
   if (value)
147
0
   {
148
0
      int i;
149
0
      opus_uint16 icdf[8];
150
0
      icdf[0] = IMAX(7, decay);
151
0
      for (i=1;i<7;i++)
152
0
      {
153
0
         icdf[i] = IMAX(7-i, (icdf[i-1] * (opus_int32)decay) >> 15);
154
0
      }
155
0
      icdf[7] = 0;
156
0
      value--;
157
0
      do {
158
0
         ec_enc_icdf16(enc, IMIN(value, 7), icdf, 15);
159
0
         value -= 7;
160
0
      } while (value >= 0);
161
0
   }
162
0
}
163
164
int ec_laplace_decode_p0(ec_dec *dec, opus_uint16 p0, opus_uint16 decay)
165
0
{
166
0
   int s;
167
0
   int value;
168
0
   opus_uint16 sign_icdf[3];
169
0
   sign_icdf[0] = 32768-p0;
170
0
   sign_icdf[1] = sign_icdf[0]/2;
171
0
   sign_icdf[2] = 0;
172
0
   s = ec_dec_icdf16(dec, sign_icdf, 15);
173
0
   if (s==2) s = -1;
174
0
   if (s != 0)
175
0
   {
176
0
      int i;
177
0
      int v;
178
0
      opus_uint16 icdf[8];
179
0
      icdf[0] = IMAX(7, decay);
180
0
      for (i=1;i<7;i++)
181
0
      {
182
0
         icdf[i] = IMAX(7-i, (icdf[i-1] * (opus_int32)decay) >> 15);
183
0
      }
184
0
      icdf[7] = 0;
185
0
      value = 1;
186
0
      do {
187
0
         v = ec_dec_icdf16(dec, icdf, 15);
188
0
         value += v;
189
0
      } while (v == 7);
190
0
      return s*value;
191
0
   } else return 0;
192
0
}
193
194
#if 0
195
196
#include <stdio.h>
197
#define NB_VALS 10
198
#define DATA_SIZE 10000
199
int main() {
200
   ec_enc enc;
201
   ec_dec dec;
202
   unsigned char *ptr;
203
   int i;
204
   int decay, p0;
205
   int val[NB_VALS] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
206
   /*for (i=0;i<NB_VALS;i++) {
207
      val[i] = -log(rand()/(float)RAND_MAX);
208
      if (rand()%2) val[i] = -val[i];
209
   }*/
210
   p0 = 16000;
211
   decay = 16000;
212
   ptr = (unsigned char *)malloc(DATA_SIZE);
213
   ec_enc_init(&enc,ptr,DATA_SIZE);
214
   for (i=0;i<NB_VALS;i++) {
215
      printf("%d ", val[i]);
216
   }
217
   printf("\n");
218
   for (i=0;i<NB_VALS;i++) {
219
      ec_laplace_encode_p0(&enc, val[i], p0, decay);
220
   }
221
222
   ec_enc_done(&enc);
223
224
   ec_dec_init(&dec,ec_get_buffer(&enc),ec_range_bytes(&enc));
225
226
   for (i=0;i<NB_VALS;i++) {
227
      val[i] = ec_laplace_decode_p0(&dec, p0, decay);
228
   }
229
   for (i=0;i<NB_VALS;i++) {
230
      printf("%d ", val[i]);
231
   }
232
   printf("\n");
233
}
234
235
#endif