Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst-libs / gst / fft / kiss_fftr_s16.c
1 /*
2 Copyright (c) 2003-2004, Mark Borgerding
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10     * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 */
14
15 #include "kiss_fftr_s16.h"
16 #include "_kiss_fft_guts_s16.h"
17
18 struct kiss_fftr_s16_state
19 {
20   kiss_fft_s16_cfg substate;
21   kiss_fft_s16_cpx *tmpbuf;
22   kiss_fft_s16_cpx *super_twiddles;
23 #ifdef USE_SIMD
24   long pad;
25 #endif
26 };
27
28 kiss_fftr_s16_cfg
29 kiss_fftr_s16_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem)
30 {
31   int i;
32   kiss_fftr_s16_cfg st = NULL;
33   size_t subsize, memneeded;
34
35   if (nfft & 1) {
36     fprintf (stderr, "Real FFT optimization must be even.\n");
37     return NULL;
38   }
39   nfft >>= 1;
40
41   kiss_fft_s16_alloc (nfft, inverse_fft, NULL, &subsize);
42   memneeded = ALIGN_STRUCT (sizeof (struct kiss_fftr_s16_state))
43       + ALIGN_STRUCT (subsize)
44       + sizeof (kiss_fft_s16_cpx) * (nfft * 3 / 2);
45
46   if (lenmem == NULL) {
47     st = (kiss_fftr_s16_cfg) KISS_FFT_S16_MALLOC (memneeded);
48   } else {
49     if (*lenmem >= memneeded)
50       st = (kiss_fftr_s16_cfg) mem;
51     *lenmem = memneeded;
52   }
53   if (!st)
54     return NULL;
55
56   st->substate = (kiss_fft_s16_cfg) (((char *) st) + ALIGN_STRUCT (sizeof (struct kiss_fftr_s16_state)));       /*just beyond kiss_fftr_s16_state struct */
57   st->tmpbuf =
58       (kiss_fft_s16_cpx *) (((char *) st->substate) + ALIGN_STRUCT (subsize));
59   st->super_twiddles = st->tmpbuf + nfft;
60   kiss_fft_s16_alloc (nfft, inverse_fft, st->substate, &subsize);
61
62   for (i = 0; i < nfft / 2; ++i) {
63     double phase =
64         -3.14159265358979323846264338327 * ((double) (i + 1) / nfft + .5);
65
66     if (inverse_fft)
67       phase *= -1;
68     kf_cexp (st->super_twiddles + i, phase);
69   }
70   return st;
71 }
72
73 void
74 kiss_fftr_s16 (kiss_fftr_s16_cfg st, const kiss_fft_s16_scalar * timedata,
75     kiss_fft_s16_cpx * freqdata)
76 {
77   /* input buffer timedata is stored row-wise */
78   int k, ncfft;
79   kiss_fft_s16_cpx fpnk, fpk, f1k, f2k, tw, tdc;
80
81   if (st->substate->inverse) {
82     fprintf (stderr, "kiss fft usage error: improper alloc\n");
83     exit (1);
84   }
85
86   ncfft = st->substate->nfft;
87
88   /*perform the parallel fft of two real signals packed in real,imag */
89   kiss_fft_s16 (st->substate, (const kiss_fft_s16_cpx *) timedata, st->tmpbuf);
90   /* The real part of the DC element of the frequency spectrum in st->tmpbuf
91    * contains the sum of the even-numbered elements of the input time sequence
92    * The imag part is the sum of the odd-numbered elements
93    *
94    * The sum of tdc.r and tdc.i is the sum of the input time sequence. 
95    *      yielding DC of input time sequence
96    * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 
97    *      yielding Nyquist bin of input time sequence
98    */
99
100   tdc.r = st->tmpbuf[0].r;
101   tdc.i = st->tmpbuf[0].i;
102   C_FIXDIV (tdc, 2);
103   CHECK_OVERFLOW_OP (tdc.r, +, tdc.i);
104   CHECK_OVERFLOW_OP (tdc.r, -, tdc.i);
105   freqdata[0].r = tdc.r + tdc.i;
106   freqdata[ncfft].r = tdc.r - tdc.i;
107 #ifdef USE_SIMD
108   freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0);
109 #else
110   freqdata[ncfft].i = freqdata[0].i = 0;
111 #endif
112
113   for (k = 1; k <= ncfft / 2; ++k) {
114     fpk = st->tmpbuf[k];
115     fpnk.r = st->tmpbuf[ncfft - k].r;
116     fpnk.i = -st->tmpbuf[ncfft - k].i;
117     C_FIXDIV (fpk, 2);
118     C_FIXDIV (fpnk, 2);
119
120     C_ADD (f1k, fpk, fpnk);
121     C_SUB (f2k, fpk, fpnk);
122     C_MUL (tw, f2k, st->super_twiddles[k - 1]);
123
124     freqdata[k].r = HALF_OF (f1k.r + tw.r);
125     freqdata[k].i = HALF_OF (f1k.i + tw.i);
126     freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r);
127     freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i);
128   }
129 }
130
131 void
132 kiss_fftri_s16 (kiss_fftr_s16_cfg st, const kiss_fft_s16_cpx * freqdata,
133     kiss_fft_s16_scalar * timedata)
134 {
135   /* input buffer timedata is stored row-wise */
136   int k, ncfft;
137
138   if (st->substate->inverse == 0) {
139     fprintf (stderr, "kiss fft usage error: improper alloc\n");
140     exit (1);
141   }
142
143   ncfft = st->substate->nfft;
144
145   st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
146   st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
147   C_FIXDIV (st->tmpbuf[0], 2);
148
149   for (k = 1; k <= ncfft / 2; ++k) {
150     kiss_fft_s16_cpx fk, fnkc, fek, fok, tmp;
151
152     fk = freqdata[k];
153     fnkc.r = freqdata[ncfft - k].r;
154     fnkc.i = -freqdata[ncfft - k].i;
155     C_FIXDIV (fk, 2);
156     C_FIXDIV (fnkc, 2);
157
158     C_ADD (fek, fk, fnkc);
159     C_SUB (tmp, fk, fnkc);
160     C_MUL (fok, tmp, st->super_twiddles[k - 1]);
161     C_ADD (st->tmpbuf[k], fek, fok);
162     C_SUB (st->tmpbuf[ncfft - k], fek, fok);
163 #ifdef USE_SIMD
164     st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0);
165 #else
166     st->tmpbuf[ncfft - k].i *= -1;
167 #endif
168   }
169   kiss_fft_s16 (st->substate, st->tmpbuf, (kiss_fft_s16_cpx *) timedata);
170 }