uploaded original spice-server-0.12.4 and celt-0.5.1.3
[sdk/emulator/libs/spice-server.git] / lib / celt-0.5.1.3 / libcelt / pitch.c
1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
2 */
3 /**
4    @file pitch.c
5    @brief Pitch analysis
6  */
7
8 /*
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12    
13    - Redistributions of source code must retain the above copyright
14    notice, this list of conditions and the following disclaimer.
15    
16    - Redistributions in binary form must reproduce the above copyright
17    notice, this list of conditions and the following disclaimer in the
18    documentation and/or other materials provided with the distribution.
19    
20    - Neither the name of the Xiph.org Foundation nor the names of its
21    contributors may be used to endorse or promote products derived from
22    this software without specific prior written permission.
23    
24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
28    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 /*#include "_kiss_fft_guts.h"
43 #include "kiss_fftr.h"*/
44 #include "kfft_single.h"
45
46 #include "pitch.h"
47 #include "psy.h"
48 #include "os_support.h"
49 #include "mathops.h"
50 #include "modes.h"
51 #include "stack_alloc.h"
52
53 kiss_fftr_cfg pitch_state_alloc(int max_lag)
54 {
55    return real16_fft_alloc(max_lag);
56 }
57
58 void pitch_state_free(kiss_fftr_cfg st)
59 {
60    real16_fft_free(st);
61 }
62
63 #ifdef FIXED_POINT
64 static void normalise16(celt_word16_t *x, int len, celt_word16_t val)
65 {
66    int i;
67    celt_word16_t maxabs;
68    maxabs = celt_maxabs16(x,len);
69    if (maxabs > val)
70    {
71       int shift = 0;
72       while (maxabs > val)
73       {
74          maxabs >>= 1;
75          shift++;
76       }
77       if (shift==0)
78          return;
79       i=0;
80       do{
81          x[i] = SHR16(x[i], shift);
82       } while (++i<len);
83    } else {
84       int shift=0;
85       if (maxabs == 0)
86          return;
87       val >>= 1;
88       while (maxabs < val)
89       {
90          val >>= 1;
91          shift++;
92       }
93       if (shift==0)
94          return;
95       i=0;
96       do{
97          x[i] = SHL16(x[i], shift);
98       } while (++i<len);
99    }
100 }
101 #else
102 #define normalise16(x,len,val)
103 #endif
104
105 #define INPUT_SHIFT 15
106
107 void find_spectral_pitch(const CELTMode *m, kiss_fftr_cfg fft, const struct PsyDecay *decay, const celt_sig_t * restrict x, const celt_sig_t * restrict y, const celt_word16_t * restrict window, celt_word16_t * restrict spectrum, int len, int max_pitch, int *pitch)
108 {
109    int c, i;
110    VARDECL(celt_word16_t, _X);
111    VARDECL(celt_word16_t, _Y);
112    const celt_word16_t * restrict wptr;
113 #ifndef SHORTCUTS
114    VARDECL(celt_mask_t, curve);
115 #endif
116    celt_word16_t * restrict X, * restrict Y;
117    celt_word16_t * restrict Xptr, * restrict Yptr;
118    const celt_sig_t * restrict yptr;
119    int n2;
120    int L2;
121    const int C = CHANNELS(m);
122    const int overlap = OVERLAP(m);
123    const int lag = MAX_PERIOD;
124    SAVE_STACK;
125    n2 = lag>>1;
126    L2 = len>>1;
127    ALLOC(_X, lag, celt_word16_t);
128    X = _X;
129 #ifndef SHORTCUTS
130    ALLOC(curve, n2, celt_mask_t);
131 #endif
132    CELT_MEMSET(X,0,lag);
133    /* Sum all channels of the current frame and copy into X in bit-reverse order */
134    for (c=0;c<C;c++)
135    {
136       const celt_sig_t * restrict xptr = &x[c];
137       for (i=0;i<L2;i++)
138       {
139          X[2*BITREV(fft,i)] += SHR32(*xptr,INPUT_SHIFT);
140          xptr += C;
141          X[2*BITREV(fft,i)+1] += SHR32(*xptr,INPUT_SHIFT);
142          xptr += C;
143       }
144    }
145    /* Applying the window in the bit-reverse domain. It's a bit weird, but it
146       can help save memory */
147    wptr = window;
148    for (i=0;i<overlap>>1;i++)
149    {
150       X[2*BITREV(fft,i)]        = MULT16_16_Q15(wptr[0], X[2*BITREV(fft,i)]);
151       X[2*BITREV(fft,i)+1]      = MULT16_16_Q15(wptr[1], X[2*BITREV(fft,i)+1]);
152       X[2*BITREV(fft,L2-i-1)]   = MULT16_16_Q15(wptr[1], X[2*BITREV(fft,L2-i-1)]);
153       X[2*BITREV(fft,L2-i-1)+1] = MULT16_16_Q15(wptr[0], X[2*BITREV(fft,L2-i-1)+1]);
154       wptr += 2;
155    }
156    normalise16(X, lag, 8192);
157    /*for (i=0;i<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
158    /* Forward real FFT (in-place) */
159    real16_fft_inplace(fft, X, lag);
160
161    if (spectrum)
162    {
163       for (i=0;i<lag/4;i++)
164       {
165          spectrum[2*i] = X[4*i];
166          spectrum[2*i+1] = X[4*i+1];
167       }
168    }
169 #ifndef SHORTCUTS
170    compute_masking(decay, X, curve, lag);
171 #endif
172    
173    /* Deferred allocation to reduce peak stack usage */
174    ALLOC(_Y, lag, celt_word16_t);
175    Y = _Y;
176    yptr = &y[0];
177    /* Copy first channel of the past audio into Y in bit-reverse order */
178    for (i=0;i<n2;i++)
179    {
180       Y[2*BITREV(fft,i)] = SHR32(*yptr,INPUT_SHIFT);
181       yptr += C;
182       Y[2*BITREV(fft,i)+1] = SHR32(*yptr,INPUT_SHIFT);
183       yptr += C;
184    }
185    /* Add remaining channels into Y in bit-reverse order */
186    for (c=1;c<C;c++)
187    {
188       yptr = &y[c];
189       for (i=0;i<n2;i++)
190       {
191          Y[2*BITREV(fft,i)] += SHR32(*yptr,INPUT_SHIFT);
192          yptr += C;
193          Y[2*BITREV(fft,i)+1] += SHR32(*yptr,INPUT_SHIFT);
194          yptr += C;
195       }
196    }
197    normalise16(Y, lag, 8192);
198    /* Forward real FFT (in-place) */
199    real16_fft_inplace(fft, Y, lag);
200
201    /* Compute cross-spectrum using the inverse masking curve as weighting */
202    Xptr = &X[2];
203    Yptr = &Y[2];
204    for (i=1;i<n2;i++)
205    {
206       celt_word16_t Xr, Xi, n;
207       /* weight = 1/sqrt(curve) */
208       Xr = Xptr[0];
209       Xi = Xptr[1];
210 #ifdef SHORTCUTS
211       /*n = SHR32(32767,(celt_ilog2(EPSILON+curve[i])>>1));*/
212       n = 1+(8192>>(celt_ilog2(1+MULT16_16(Xr,Xr)+MULT16_16(Xi,Xi))>>1));
213       /* Pre-multiply X by n, so we can keep everything in 16 bits */
214       Xr = MULT16_16_16(n, Xr);
215       Xi = MULT16_16_16(n, Xi);
216 #else
217       n = celt_rsqrt(EPSILON+curve[i]);
218       /* Pre-multiply X by n, so we can keep everything in 16 bits */
219       Xr = EXTRACT16(SHR32(MULT16_16(n, Xr),3));
220       Xi = EXTRACT16(SHR32(MULT16_16(n, Xi),3));
221 #endif
222       /* Cross-spectrum between X and conj(Y) */
223       *Xptr++ = ADD16(MULT16_16_Q15(Xr, Yptr[0]), MULT16_16_Q15(Xi,Yptr[1]));
224       *Xptr++ = SUB16(MULT16_16_Q15(Xr, Yptr[1]), MULT16_16_Q15(Xi,Yptr[0]));
225       Yptr += 2;
226    }
227    /*printf ("\n");*/
228    X[0] = X[1] = 0;
229    /*for (i=0;i<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
230    normalise16(X, lag, 50);
231    /* Inverse half-complex to real FFT gives us the correlation */
232    real16_ifft(fft, X, Y, lag);
233    
234    /* The peak in the correlation gives us the pitch */
235    *pitch = find_max16(Y, max_pitch);
236    RESTORE_STACK;
237 }