1 /* (C) 2008 Jean-Marc Valin, CSIRO
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
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.
32 /* This is a simple MDCT implementation that uses a N/4 complex FFT
33 to do most of the work. It should be relatively straightforward to
34 plug in pretty much and FFT here.
36 This replaces the Vorbis FFT (and uses the exact same API), which
37 was a bit too messy and that was ending up duplicating code
38 (might as well use the same FFT everywhere).
40 The algorithm is similar to (and inspired from) Fabrice Bellard's
41 MDCT implementation in FFMPEG, but has differences in signs, ordering
42 and scaling in many places.
52 #include "os_support.h"
53 #include "_kiss_fft_guts.h"
56 #define M_PI 3.141592653
59 void mdct_init(mdct_lookup *l,int N)
66 l->kfft = kiss_fft_alloc(N4, NULL, NULL);
67 l->trig = (kiss_twiddle_scalar*)celt_alloc(N2*sizeof(kiss_twiddle_scalar));
68 /* We have enough points that sine isn't necessary */
69 #if defined(FIXED_POINT)
70 #if defined(DOUBLE_PRECISION) & !defined(MIXED_PRECISION)
72 l->trig[i] = SAMP_MAX*cos(2*M_PI*(i+1./8.)/N);
75 l->trig[i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(i,17),16386),N));
79 l->trig[i] = cos(2*M_PI*(i+1./8.)/N);
83 void mdct_clear(mdct_lookup *l)
85 kiss_fft_free(l->kfft);
89 void mdct_forward(mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar *out)
93 VARDECL(kiss_fft_scalar *f);
97 ALLOC(f, N2, kiss_fft_scalar);
99 /* Consider the input to be compused of four blocks: [a, b, c, d] */
100 /* Shuffle, fold, pre-rotate (part 1) */
103 kiss_fft_scalar re, im;
104 /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/
105 re = -.5*(in[N2+N4+2*i] + in[N2+N4-2*i-1]);
106 im = -.5*(in[N4+2*i] - in[N4-2*i-1]);
107 out[2*i] = S_MUL(re,l->trig[i]) - S_MUL(im,l->trig[i+N4]);
108 out[2*i+1] = S_MUL(im,l->trig[i]) + S_MUL(re,l->trig[i+N4]);
112 kiss_fft_scalar re, im;
113 /* Real part arranged as a-bR, Imag part arranged as -c-dR */
114 re = .5*(in[2*i-N4] - in[N2+N4-2*i-1]);
115 im = -.5*(in[N4+2*i] + in[N+N4-2*i-1]);
116 out[2*i] = S_MUL(re,l->trig[i]) - S_MUL(im,l->trig[i+N4]);
117 out[2*i+1] = S_MUL(im,l->trig[i]) + S_MUL(re,l->trig[i+N4]);
120 /* N/4 complex FFT, which should normally down-scale by 4/N (but doesn't now) */
121 kiss_fft(l->kfft, (const kiss_fft_cpx *)out, (kiss_fft_cpx *)f);
123 /* Post-rotate and apply the scaling if the FFT doesn't to it itself */
126 out[2*i] = -S_MUL(f[2*i+1],l->trig[i+N4]) + S_MUL(f[2*i] ,l->trig[i]);
127 out[N2-1-2*i] = -S_MUL(f[2*i] ,l->trig[i+N4]) - S_MUL(f[2*i+1],l->trig[i]);
132 void mdct_backward(mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar *out)
136 VARDECL(kiss_fft_scalar *f);
141 ALLOC(f, N2, kiss_fft_scalar);
146 out[2*i] = -S_MUL(in[N2-2*i-1], l->trig[i]) - S_MUL(in[2*i],l->trig[i+N4]);
147 out[2*i+1] = S_MUL(in[N2-2*i-1], l->trig[i+N4]) - S_MUL(in[2*i],l->trig[i]);
150 /* Inverse N/4 complex FFT. This one should *not* downscale even in fixed-point */
151 kiss_ifft(l->kfft, (const kiss_fft_cpx *)out, (kiss_fft_cpx *)f);
156 kiss_fft_scalar re, im;
159 /* We'd scale up by 2 here, but instead it's done when mixing the windows */
160 f[2*i] = S_MUL(re,l->trig[i]) + S_MUL(im,l->trig[i+N4]);
161 f[2*i+1] = S_MUL(im,l->trig[i]) - S_MUL(re,l->trig[i+N4]);
163 /* De-shuffle the components for the middle of the window only */
164 for(i = 0; i < N4; i++)
166 out[N4+2*i] =-f[2*i];
167 out[N4+2*i+1] = f[N2-2*i-1];
170 /* Mirror on both sides for TDAC */
171 for(i = 0; i < N4; i++)
173 out[i] =-out[N2-i-1];
174 out[N-i-1] = out[N2+i];