blob: e380900704941ead31c8b972b5738acfbf5da3df [file] [log] [blame]
Jean-Marc Valin41af4212007-11-30 18:35:37 +11001/* (C) 2007 Jean-Marc Valin, CSIRO
2*/
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
32#include <math.h>
33#include <stdlib.h>
Jean-Marc Valin29ccab82007-12-06 15:39:38 +110034#include "cwrs.h"
Jean-Marc Valin9cace642007-12-06 17:44:09 +110035#include "vq.h"
Jean-Marc Valin41af4212007-11-30 18:35:37 +110036
Jean-Marc Valin41af4212007-11-30 18:35:37 +110037
38/* Improved algebraic pulse-base quantiser. The signal x is replaced by the sum of the pitch
39 a combination of pulses such that its norm is still equal to 1. The only difference with
40 the quantiser above is that the search is more complete. */
Jean-Marc Valin46014ca2007-12-14 13:47:04 +110041void alg_quant(float *x, float *W, int N, int K, float *p, float alpha, ec_enc *enc)
Jean-Marc Valin41af4212007-11-30 18:35:37 +110042{
Jean-Marc Valin4a897682007-12-12 00:45:15 +110043 int L = 3;
Jean-Marc Valin41af4212007-11-30 18:35:37 +110044 //float tata[200];
45 float y[L][N];
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110046 int iy[L][N];
Jean-Marc Valin41af4212007-11-30 18:35:37 +110047 //float tata2[200];
48 float ny[L][N];
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110049 int iny[L][N];
Jean-Marc Valin41af4212007-11-30 18:35:37 +110050 int i, j, m;
51 float xy[L], nxy[L];
52 float yy[L], nyy[L];
53 float yp[L], nyp[L];
54 float best_scores[L];
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110055 float Rpp=0, Rxp=0;
Jean-Marc Valin41af4212007-11-30 18:35:37 +110056 float gain[L];
57 int maxL = 1;
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110058
Jean-Marc Valin41af4212007-11-30 18:35:37 +110059 for (j=0;j<N;j++)
60 Rpp += p[j]*p[j];
Jean-Marc Valinfc08d0a2007-12-07 13:26:15 +110061 //if (Rpp>.01)
62 // alpha = (1-sqrt(1-Rpp))/Rpp;
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110063 for (j=0;j<N;j++)
64 Rxp += x[j]*p[j];
Jean-Marc Valin41af4212007-11-30 18:35:37 +110065 for (m=0;m<L;m++)
66 for (i=0;i<N;i++)
67 y[m][i] = 0;
68
69 for (m=0;m<L;m++)
70 for (i=0;i<N;i++)
71 ny[m][i] = 0;
72
73 for (m=0;m<L;m++)
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110074 for (i=0;i<N;i++)
75 iy[m][i] = iny[m][i] = 0;
76
77 for (m=0;m<L;m++)
Jean-Marc Valin41af4212007-11-30 18:35:37 +110078 xy[m] = yy[m] = yp[m] = gain[m] = 0;
79
80 for (i=0;i<K;i++)
81 {
82 int L2 = L;
83 if (L>maxL)
84 {
85 L2 = maxL;
86 maxL *= N;
87 }
88 for (m=0;m<L;m++)
89 best_scores[m] = -1e10;
90
91 for (m=0;m<L2;m++)
92 {
93 for (j=0;j<N;j++)
94 {
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110095 int sign;
96 for (sign=-1;sign<=1;sign+=2)
Jean-Marc Valin41af4212007-11-30 18:35:37 +110097 {
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +110098 if (iy[m][j]*sign < 0)
99 continue;
100 //fprintf (stderr, "%d/%d %d/%d %d/%d\n", i, K, m, L2, j, N);
101 float tmp_xy, tmp_yy, tmp_yp;
102 float score;
103 float g;
104 float s = sign;
105 tmp_xy = xy[m] + s*x[j] - alpha*s*p[j]*Rxp;
106 tmp_yy = yy[m] + 2*s*y[m][j] + 1 +alpha*alpha*p[j]*p[j]*Rpp - 2*alpha*s*p[j]*yp[m] - 2*alpha*p[j]*p[j];
107 tmp_yp = yp[m] + s*p[j] *(1-alpha*Rpp);
108 g = (sqrt(tmp_yp*tmp_yp + tmp_yy - tmp_yy*Rpp) - tmp_yp)/tmp_yy;
109 score = 2*g*tmp_xy - g*g*tmp_yy;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100110
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +1100111 if (score>best_scores[L-1])
112 {
113 int k, n;
114 int id = L-1;
115 while (id > 0 && score > best_scores[id-1])
116 id--;
117
118 for (k=L-1;k>id;k--)
119 {
120 nxy[k] = nxy[k-1];
121 nyy[k] = nyy[k-1];
122 nyp[k] = nyp[k-1];
123 //fprintf(stderr, "%d %d \n", N, k);
124 for (n=0;n<N;n++)
125 ny[k][n] = ny[k-1][n];
126 for (n=0;n<N;n++)
127 iny[k][n] = iny[k-1][n];
128 gain[k] = gain[k-1];
129 best_scores[k] = best_scores[k-1];
130 }
131
132 nxy[id] = tmp_xy;
133 nyy[id] = tmp_yy;
134 nyp[id] = tmp_yp;
135 gain[id] = g;
136 for (n=0;n<N;n++)
137 ny[id][n] = y[m][n];
138 ny[id][j] += s;
139 for (n=0;n<N;n++)
140 ny[id][n] -= alpha*s*p[j]*p[n];
141
142 for (n=0;n<N;n++)
143 iny[id][n] = iy[m][n];
144 if (s>0)
145 iny[id][j] += 1;
146 else
147 iny[id][j] -= 1;
148 best_scores[id] = score;
149 }
150 }
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100151 }
152
153 }
154 int k,n;
155 for (k=0;k<L;k++)
156 {
157 xy[k] = nxy[k];
158 yy[k] = nyy[k];
159 yp[k] = nyp[k];
160 for (n=0;n<N;n++)
161 y[k][n] = ny[k][n];
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +1100162 for (n=0;n<N;n++)
163 iy[k][n] = iny[k][n];
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100164 }
165
166 }
167
168 for (i=0;i<N;i++)
169 x[i] = p[i]+gain[0]*y[0][i];
Jean-Marc Valin1e7087e2007-12-04 13:05:43 +1100170 if (0) {
171 float E=1e-15;
172 int ABS = 0;
173 for (i=0;i<N;i++)
174 ABS += abs(iy[0][i]);
175 //if (K != ABS)
176 // printf ("%d %d\n", K, ABS);
177 for (i=0;i<N;i++)
178 E += x[i]*x[i];
179 //printf ("%f\n", E);
180 E = 1/sqrt(E);
181 for (i=0;i<N;i++)
182 x[i] *= E;
183 }
Jean-Marc Valin29ccab82007-12-06 15:39:38 +1100184 int comb[K];
185 int signs[K];
Jean-Marc Valin70c8ffd2007-12-07 14:20:01 +1100186 //for (i=0;i<N;i++)
187 // printf ("%d ", iy[0][i]);
Jean-Marc Valin29ccab82007-12-06 15:39:38 +1100188 pulse2comb(N, K, comb, signs, iy[0]);
Jean-Marc Valinf8db8002007-12-11 14:52:56 +1100189 ec_enc_uint64(enc,icwrs64(N, K, comb, signs),ncwrs64(N, K));
Jean-Marc Valin4a897682007-12-12 00:45:15 +1100190
Jean-Marc Valina4833ff2008-01-10 15:34:00 +1100191 /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
192 due to the recursive computation used in quantisation */
193 if (1) {
Jean-Marc Valin4a897682007-12-12 00:45:15 +1100194 float Ryp=0;
195 float Rpp=0;
196 float Ryy=0;
197 float g=0;
198 for (i=0;i<N;i++)
199 Rpp += p[i]*p[i];
200
201 for (i=0;i<N;i++)
202 Ryp += iy[0][i]*p[i];
203
204 for (i=0;i<N;i++)
205 y[0][i] = iy[0][i] - alpha*Ryp*p[i];
206
Jean-Marc Valin4a897682007-12-12 00:45:15 +1100207 Ryp = 0;
208 for (i=0;i<N;i++)
209 Ryp += y[0][i]*p[i];
210
211 for (i=0;i<N;i++)
212 Ryy += y[0][i]*y[0][i];
213
214 g = (sqrt(Ryp*Ryp + Ryy - Ryy*Rpp) - Ryp)/Ryy;
215
216 for (i=0;i<N;i++)
217 x[i] = p[i] + g*y[0][i];
218
219 }
220
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100221}
222
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100223void alg_unquant(float *x, int N, int K, float *p, float alpha, ec_dec *dec)
224{
225 int i;
226 celt_uint64_t id;
227 int comb[K];
228 int signs[K];
229 int iy[N];
230 float y[N];
231 float Rpp=0, Ryp=0, Ryy=0;
232 float g;
Jean-Marc Valin25298f22007-12-03 15:24:11 +1100233
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100234 id = ec_dec_uint64(dec, ncwrs64(N, K));
235 cwrsi64(N, K, id, comb, signs);
236 comb2pulse(N, K, iy, comb, signs);
237 //for (i=0;i<N;i++)
238 // printf ("%d ", iy[i]);
239 for (i=0;i<N;i++)
240 Rpp += p[i]*p[i];
241
242 for (i=0;i<N;i++)
243 Ryp += iy[i]*p[i];
244
245 for (i=0;i<N;i++)
246 y[i] = iy[i] - alpha*Ryp*p[i];
247
248 /* Recompute after the projection (I think it's right) */
249 Ryp = 0;
250 for (i=0;i<N;i++)
251 Ryp += y[i]*p[i];
252
253 for (i=0;i<N;i++)
254 Ryy += y[i]*y[i];
255
256 g = (sqrt(Ryp*Ryp + Ryy - Ryy*Rpp) - Ryp)/Ryy;
257
258 for (i=0;i<N;i++)
259 x[i] = p[i] + g*y[i];
260}
261
262
263static const float pg[11] = {1.f, .75f, .65f, 0.6f, 0.6f, .6f, .55f, .55f, .5f, .5f, .5f};
264
265void intra_prediction(float *x, float *W, int N, int K, float *Y, float *P, int B, int N0, ec_enc *enc)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100266{
267 int i,j;
268 int best=0;
Jean-Marc Valin50d11162007-12-03 14:34:52 +1100269 float best_score=0;
270 float s = 1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100271 int sign;
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100272 float E;
273 for (i=0;i<N0*B-N;i+=B)
274 {
275 int j;
276 float xy=0, yy=0;
277 float score;
278 for (j=0;j<N;j++)
279 {
280 xy += x[j]*Y[i+j];
281 yy += Y[i+j]*Y[i+j];
282 }
Jean-Marc Valin50d11162007-12-03 14:34:52 +1100283 score = xy*xy/(.001+yy);
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100284 if (score > best_score)
285 {
286 best_score = score;
287 best = i;
Jean-Marc Valin50d11162007-12-03 14:34:52 +1100288 if (xy>0)
289 s = 1;
290 else
291 s = -1;
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100292 }
293 }
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100294 if (s<0)
295 sign = 1;
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100296 else
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100297 sign = 0;
298 //printf ("%d %d ", sign, best);
299 ec_enc_uint(enc,sign,2);
Jean-Marc Valin9cace642007-12-06 17:44:09 +1100300 ec_enc_uint(enc,best/B,N0-N/B);
Jean-Marc Valin50d11162007-12-03 14:34:52 +1100301 //printf ("%d %f\n", best, best_score);
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100302
303 float pred_gain;
304 if (K>10)
305 pred_gain = pg[10];
306 else
307 pred_gain = pg[K];
308 E = 1e-10;
309 for (j=0;j<N;j++)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100310 {
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100311 P[j] = s*Y[best+j];
312 E += P[j]*P[j];
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100313 }
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100314 E = pred_gain/sqrt(E);
315 for (j=0;j<N;j++)
316 P[j] *= E;
317 if (K>0)
318 {
319 for (j=0;j<N;j++)
320 x[j] -= P[j];
321 } else {
322 for (j=0;j<N;j++)
323 x[j] = P[j];
324 }
325 //printf ("quant ");
326 //for (j=0;j<N;j++) printf ("%f ", P[j]);
327
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100328}
Jean-Marc Valinfc08d0a2007-12-07 13:26:15 +1100329
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100330void intra_unquant(float *x, int N, int K, float *Y, float *P, int B, int N0, ec_dec *dec)
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100331{
Jean-Marc Valin11f01722007-12-09 01:19:36 +1100332 int j;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100333 int sign;
334 float s;
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100335 int best;
336 float E;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100337 sign = ec_dec_uint(dec, 2);
338 if (sign == 0)
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100339 s = 1;
340 else
341 s = -1;
342
343 best = B*ec_dec_uint(dec, N0-N/B);
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100344 //printf ("%d %d ", sign, best);
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100345
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100346 float pred_gain;
347 if (K>10)
348 pred_gain = pg[10];
349 else
350 pred_gain = pg[K];
351 E = 1e-10;
352 for (j=0;j<N;j++)
353 {
354 P[j] = s*Y[best+j];
355 E += P[j]*P[j];
356 }
357 E = pred_gain/sqrt(E);
358 for (j=0;j<N;j++)
359 P[j] *= E;
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100360 if (K==0)
361 {
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100362 for (j=0;j<N;j++)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100363 x[j] = P[j];
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100364 }
365}