blob: 57bc5f3dd3fd886162fe4c3a7fdb87197fc53ebd [file] [log] [blame]
Jean-Marc Valin6c3788c2010-06-20 22:48:50 -04001/* Copyright (c) 2009-2010 Xiph.Org Foundation
2 Written by Jean-Marc Valin */
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:
Jean-Marc Valine14fe902009-12-11 00:07:31 -05007
Jean-Marc Valin6c3788c2010-06-20 22:48:50 -04008 - 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
Jean-Marc Valin6c3788c2010-06-20 22:48:50 -040015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040018 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Jean-Marc Valin6c3788c2010-06-20 22:48:50 -040020 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050030#endif
Jean-Marc Valine14fe902009-12-11 00:07:31 -050031
Jean-Marc Valin2779df72011-10-04 13:26:53 -040032#include "celt_lpc.h"
Jean-Marc Valin6c3788c2010-06-20 22:48:50 -040033#include "stack_alloc.h"
34#include "mathops.h"
Jean-Marc Valine8e57a32013-05-25 02:14:25 -040035#include "pitch.h"
Jean-Marc Valin3a9699e2010-06-17 00:35:26 -040036
Jean-Marc Valin7b7f0712010-06-17 20:10:02 -040037void _celt_lpc(
Jean-Marc Valinff5f7222011-07-29 18:59:12 -040038 opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */
39const opus_val32 *ac, /* in: [0...p] autocorrelation values */
Jean-Marc Valine14fe902009-12-11 00:07:31 -050040int p
41)
42{
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040043 int i, j;
Jean-Marc Valinff5f7222011-07-29 18:59:12 -040044 opus_val32 r;
45 opus_val32 error = ac[0];
Jean-Marc Valin74128be2010-01-01 09:33:17 -050046#ifdef FIXED_POINT
Jean-Marc Valinff5f7222011-07-29 18:59:12 -040047 opus_val32 lpc[LPC_ORDER];
Jean-Marc Valin74128be2010-01-01 09:33:17 -050048#else
49 float *lpc = _lpc;
50#endif
Jean-Marc Valine14fe902009-12-11 00:07:31 -050051
Jean-Marc Valinb66080a2016-06-20 12:11:05 -040052 OPUS_CLEAR(lpc, p);
Jean-Marc Valin456eab22010-06-16 22:38:57 -040053 if (ac[0] != 0)
Jean-Marc Valine14fe902009-12-11 00:07:31 -050054 {
Jean-Marc Valin456eab22010-06-16 22:38:57 -040055 for (i = 0; i < p; i++) {
56 /* Sum up this iteration's reflection coefficient */
Jean-Marc Valinff5f7222011-07-29 18:59:12 -040057 opus_val32 rr = 0;
Jean-Marc Valin456eab22010-06-16 22:38:57 -040058 for (j = 0; j < i; j++)
59 rr += MULT32_32_Q31(lpc[j],ac[i - j]);
60 rr += SHR32(ac[i + 1],3);
Jean-Marc Valinbd82ca82010-06-17 00:11:54 -040061 r = -frac_div32(SHL32(rr,3), error);
Jean-Marc Valin456eab22010-06-16 22:38:57 -040062 /* Update LPC coefficients and total error */
63 lpc[i] = SHR32(r,3);
64 for (j = 0; j < (i+1)>>1; j++)
65 {
Jean-Marc Valinff5f7222011-07-29 18:59:12 -040066 opus_val32 tmp1, tmp2;
Jean-Marc Valin456eab22010-06-16 22:38:57 -040067 tmp1 = lpc[j];
68 tmp2 = lpc[i-1-j];
69 lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2);
70 lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1);
71 }
72
73 error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error);
Jean-Marc Valinbd82ca82010-06-17 00:11:54 -040074 /* Bail out once we get 30 dB gain */
75#ifdef FIXED_POINT
76 if (error<SHR32(ac[0],10))
Jean-Marc Valin456eab22010-06-16 22:38:57 -040077 break;
Jean-Marc Valinbd82ca82010-06-17 00:11:54 -040078#else
Gregory Maxwell60c316b2010-11-04 20:14:19 -040079 if (error<.001f*ac[0])
Jean-Marc Valinbd82ca82010-06-17 00:11:54 -040080 break;
81#endif
Jean-Marc Valine14fe902009-12-11 00:07:31 -050082 }
Jean-Marc Valine14fe902009-12-11 00:07:31 -050083 }
Jean-Marc Valin303b3b62009-12-30 22:40:24 -050084#ifdef FIXED_POINT
85 for (i=0;i<p;i++)
Jean-Marc Valin456eab22010-06-16 22:38:57 -040086 _lpc[i] = ROUND16(lpc[i],16);
Jean-Marc Valin303b3b62009-12-30 22:40:24 -050087#endif
Jean-Marc Valine14fe902009-12-11 00:07:31 -050088}
89
xiangmingzhuc95c9a02014-04-30 15:48:07 +080090
91void celt_fir_c(
Linfeng Zhang783ad762016-06-16 16:21:02 -070092 const opus_val16 *x,
Jean-Marc Valinff5f7222011-07-29 18:59:12 -040093 const opus_val16 *num,
Linfeng Zhang783ad762016-06-16 16:21:02 -070094 opus_val16 *y,
Jean-Marc Valine14fe902009-12-11 00:07:31 -050095 int N,
96 int ord,
xiangmingzhuc95c9a02014-04-30 15:48:07 +080097 int arch)
Jean-Marc Valine14fe902009-12-11 00:07:31 -050098{
99 int i,j;
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400100 VARDECL(opus_val16, rnum);
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400101 SAVE_STACK;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500102
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400103 ALLOC(rnum, ord, opus_val16);
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400104 for(i=0;i<ord;i++)
105 rnum[i] = num[ord-i-1];
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400106 for (i=0;i<N-3;i+=4)
107 {
Jean-Marc Valinad155782017-02-18 17:04:24 -0500108 opus_val32 sum[4];
109 sum[0] = SHL32(EXTEND32(x[i ]), SIG_SHIFT);
110 sum[1] = SHL32(EXTEND32(x[i+1]), SIG_SHIFT),
111 sum[2] = SHL32(EXTEND32(x[i+2]), SIG_SHIFT);
112 sum[3] = SHL32(EXTEND32(x[i+3]), SIG_SHIFT);
Linfeng Zhang783ad762016-06-16 16:21:02 -0700113 xcorr_kernel(rnum, x+i-ord, sum, ord, arch);
Jean-Marc Valin414eef52017-02-17 19:28:28 -0500114 y[i ] = ROUND16(sum[0], SIG_SHIFT);
115 y[i+1] = ROUND16(sum[1], SIG_SHIFT);
116 y[i+2] = ROUND16(sum[2], SIG_SHIFT);
117 y[i+3] = ROUND16(sum[3], SIG_SHIFT);
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400118 }
119 for (;i<N;i++)
120 {
Jean-Marc Valin414eef52017-02-17 19:28:28 -0500121 opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400122 for (j=0;j<ord;j++)
Linfeng Zhang783ad762016-06-16 16:21:02 -0700123 sum = MAC16_16(sum,rnum[j],x[i+j-ord]);
Jean-Marc Valin414eef52017-02-17 19:28:28 -0500124 y[i] = ROUND16(sum, SIG_SHIFT);
Jean-Marc Valine2374a82013-05-25 04:25:54 -0400125 }
Jean-Marc Valin0fa5fa82013-05-25 18:50:01 -0400126 RESTORE_STACK;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500127}
128
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400129void celt_iir(const opus_val32 *_x,
Jean-Marc Valinff5f7222011-07-29 18:59:12 -0400130 const opus_val16 *den,
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400131 opus_val32 *_y,
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500132 int N,
133 int ord,
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800134 opus_val16 *mem,
135 int arch)
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500136{
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400137#ifdef SMALL_FOOTPRINT
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500138 int i,j;
Timothy B. Terriberry020a0a42014-10-03 21:11:51 -0700139 (void)arch;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500140 for (i=0;i<N;i++)
141 {
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400142 opus_val32 sum = _x[i];
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500143 for (j=0;j<ord;j++)
144 {
Jean-Marc Valin74128be2010-01-01 09:33:17 -0500145 sum -= MULT16_16(den[j],mem[j]);
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500146 }
147 for (j=ord-1;j>=1;j--)
148 {
149 mem[j]=mem[j-1];
150 }
Jean-Marc Valina0bd7d32016-07-22 09:13:00 -0400151 mem[0] = SROUND16(sum, SIG_SHIFT);
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400152 _y[i] = sum;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500153 }
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400154#else
155 int i,j;
156 VARDECL(opus_val16, rden);
157 VARDECL(opus_val16, y);
158 SAVE_STACK;
159
160 celt_assert((ord&3)==0);
161 ALLOC(rden, ord, opus_val16);
162 ALLOC(y, N+ord, opus_val16);
163 for(i=0;i<ord;i++)
164 rden[i] = den[ord-i-1];
165 for(i=0;i<ord;i++)
166 y[i] = -mem[ord-i-1];
167 for(;i<N+ord;i++)
168 y[i]=0;
169 for (i=0;i<N-3;i+=4)
170 {
Jean-Marc Valin068cbd82013-05-26 20:08:35 -0400171 /* Unroll by 4 as if it were an FIR filter */
Jean-Marc Valin0fed0742013-05-26 20:29:44 -0400172 opus_val32 sum[4];
173 sum[0]=_x[i];
174 sum[1]=_x[i+1];
175 sum[2]=_x[i+2];
176 sum[3]=_x[i+3];
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800177 xcorr_kernel(rden, y+i, sum, ord, arch);
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400178
Jean-Marc Valin068cbd82013-05-26 20:08:35 -0400179 /* Patch up the result to compensate for the fact that this is an IIR */
Jean-Marc Valina0bd7d32016-07-22 09:13:00 -0400180 y[i+ord ] = -SROUND16(sum[0],SIG_SHIFT);
Jean-Marc Valin068cbd82013-05-26 20:08:35 -0400181 _y[i ] = sum[0];
182 sum[1] = MAC16_16(sum[1], y[i+ord ], den[0]);
Jean-Marc Valina0bd7d32016-07-22 09:13:00 -0400183 y[i+ord+1] = -SROUND16(sum[1],SIG_SHIFT);
Jean-Marc Valin068cbd82013-05-26 20:08:35 -0400184 _y[i+1] = sum[1];
185 sum[2] = MAC16_16(sum[2], y[i+ord+1], den[0]);
186 sum[2] = MAC16_16(sum[2], y[i+ord ], den[1]);
Jean-Marc Valina0bd7d32016-07-22 09:13:00 -0400187 y[i+ord+2] = -SROUND16(sum[2],SIG_SHIFT);
Jean-Marc Valin068cbd82013-05-26 20:08:35 -0400188 _y[i+2] = sum[2];
189
190 sum[3] = MAC16_16(sum[3], y[i+ord+2], den[0]);
191 sum[3] = MAC16_16(sum[3], y[i+ord+1], den[1]);
192 sum[3] = MAC16_16(sum[3], y[i+ord ], den[2]);
Jean-Marc Valina0bd7d32016-07-22 09:13:00 -0400193 y[i+ord+3] = -SROUND16(sum[3],SIG_SHIFT);
Jean-Marc Valin068cbd82013-05-26 20:08:35 -0400194 _y[i+3] = sum[3];
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400195 }
196 for (;i<N;i++)
197 {
198 opus_val32 sum = _x[i];
199 for (j=0;j<ord;j++)
200 sum -= MULT16_16(rden[j],y[i+j]);
Jean-Marc Valina0bd7d32016-07-22 09:13:00 -0400201 y[i+ord] = SROUND16(sum,SIG_SHIFT);
Jean-Marc Valin531cf592013-05-25 07:41:55 -0400202 _y[i] = sum;
203 }
204 for(i=0;i<ord;i++)
205 mem[i] = _y[N-i-1];
Jean-Marc Valin0fa5fa82013-05-25 18:50:01 -0400206 RESTORE_STACK;
John Ridgese50e8082013-06-06 23:12:57 -0400207#endif
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500208}
209
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400210int _celt_autocorr(
Jean-Marc Valinff5f7222011-07-29 18:59:12 -0400211 const opus_val16 *x, /* in: [0...n-1] samples x */
212 opus_val32 *ac, /* out: [0...lag-1] ac values */
213 const opus_val16 *window,
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500214 int overlap,
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400215 int lag,
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500216 int n,
217 int arch
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500218 )
219{
Jean-Marc Valinff5f7222011-07-29 18:59:12 -0400220 opus_val32 d;
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400221 int i, k;
Jean-Marc Valine8e57a32013-05-25 02:14:25 -0400222 int fastN=n-lag;
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400223 int shift;
224 const opus_val16 *xptr;
Jean-Marc Valinff5f7222011-07-29 18:59:12 -0400225 VARDECL(opus_val16, xx);
Thorvald Natvigb8002a02009-12-11 13:19:09 +0100226 SAVE_STACK;
Jean-Marc Valinff5f7222011-07-29 18:59:12 -0400227 ALLOC(xx, n, opus_val16);
Gregory Maxwell5d5875a2011-10-03 21:07:39 -0400228 celt_assert(n>0);
229 celt_assert(overlap>=0);
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400230 if (overlap == 0)
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500231 {
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400232 xptr = x;
233 } else {
234 for (i=0;i<n;i++)
235 xx[i] = x[i];
236 for (i=0;i<overlap;i++)
237 {
238 xx[i] = MULT16_16_Q15(x[i],window[i]);
239 xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]);
240 }
241 xptr = xx;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500242 }
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400243 shift=0;
Jean-Marc Valin456eab22010-06-16 22:38:57 -0400244#ifdef FIXED_POINT
245 {
Timothy B. Terriberry85ede2c2013-05-22 15:26:12 -0700246 opus_val32 ac0;
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400247 ac0 = 1+(n<<7);
248 if (n&1) ac0 += SHR32(MULT16_16(xptr[0],xptr[0]),9);
Timothy B. Terriberry85ede2c2013-05-22 15:26:12 -0700249 for(i=(n&1);i<n;i+=2)
250 {
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400251 ac0 += SHR32(MULT16_16(xptr[i],xptr[i]),9);
252 ac0 += SHR32(MULT16_16(xptr[i+1],xptr[i+1]),9);
Timothy B. Terriberry85ede2c2013-05-22 15:26:12 -0700253 }
Jean-Marc Valin0da0d912010-06-17 07:32:20 -0400254
Jean-Marc Valine1be1922011-11-28 22:48:01 -0500255 shift = celt_ilog2(ac0)-30+10;
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400256 shift = (shift)/2;
257 if (shift>0)
258 {
259 for(i=0;i<n;i++)
260 xx[i] = PSHR32(xptr[i], shift);
261 xptr = xx;
262 } else
263 shift = 0;
Jean-Marc Valin456eab22010-06-16 22:38:57 -0400264 }
265#endif
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500266 celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1, arch);
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400267 for (k=0;k<=lag;k++)
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500268 {
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400269 for (i = k+fastN, d = 0; i < n; i++)
270 d = MAC16_16(d, xptr[i], xptr[i-k]);
271 ac[k] += d;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500272 }
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400273#ifdef FIXED_POINT
274 shift = 2*shift;
275 if (shift<=0)
276 ac[0] += SHL32((opus_int32)1, -shift);
277 if (ac[0] < 268435456)
278 {
279 int shift2 = 29 - EC_ILOG(ac[0]);
280 for (i=0;i<=lag;i++)
281 ac[i] = SHL32(ac[i], shift2);
282 shift -= shift2;
283 } else if (ac[0] >= 536870912)
284 {
285 int shift2=1;
286 if (ac[0] >= 1073741824)
287 shift2++;
288 for (i=0;i<=lag;i++)
289 ac[i] = SHR32(ac[i], shift2);
290 shift += shift2;
291 }
292#endif
Jean-Marc Valin456eab22010-06-16 22:38:57 -0400293
Thorvald Natvigb8002a02009-12-11 13:19:09 +0100294 RESTORE_STACK;
Jean-Marc Valin6a7ee7f2013-08-28 17:55:34 -0400295 return shift;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500296}