Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / openmax_dl / dl / sp / src / arm / omxSP_FFTInit_R_F32.c
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  *  This is a modification of omxSP_FFTInit_R_S32.c to support float
11  *  instead of S32.
12  */
13
14 #include <stdint.h>
15  
16 #include "dl/api/arm/armOMX.h"
17 #include "dl/api/omxtypes.h"
18 #include "dl/sp/api/armSP.h"
19 #include "dl/sp/api/omxSP.h"
20
21 /**
22  * Function: omxSP_FFTInit_R_F32
23  *
24  * Description:
25  * Initialize the real forward-FFT specification information struct.
26  *
27  * Remarks:
28  * This function is used to initialize the specification structures
29  * for functions <ippsFFTFwd_RToCCS_F32_Sfs> and
30  * <ippsFFTInv_CCSToR_F32_Sfs>. Memory for *pFFTSpec must be
31  * allocated prior to calling this function. The number of bytes
32  * required for *pFFTSpec can be determined using
33  * <FFTGetBufSize_R_F32>.
34  *
35  * Parameters:
36  * [in]  order       base-2 logarithm of the desired block length;
37  *                         valid in the range [1,12].  ([1,15] if
38  *                         BIG_FFT_TABLE is defined.)
39  * [out] pFFTFwdSpec pointer to the initialized specification structure.
40  *
41  * Return Value:
42  * Standard omxError result. See enumeration for possible result codes.
43  *
44  */
45 OMXResult omxSP_FFTInit_R_F32(OMXFFTSpec_R_F32* pFFTSpec, OMX_INT order) {
46   OMX_INT i;
47   OMX_INT j;
48   OMX_FC32* pTwiddle;
49   OMX_FC32* pTwiddle1;
50   OMX_FC32* pTwiddle2;
51   OMX_FC32* pTwiddle3;
52   OMX_FC32* pTwiddle4;
53   OMX_F32* pBuf;
54   OMX_U16* pBitRev;
55   OMX_U32 pTmp;
56   OMX_INT Nby2;
57   OMX_INT N;
58   OMX_INT M;
59   OMX_INT diff;
60   OMX_INT step;
61   OMX_F32 x;
62   OMX_F32 y;
63   OMX_F32 xNeg;
64   ARMsFFTSpec_R_FC32* pFFTStruct = 0;
65
66   pFFTStruct = (ARMsFFTSpec_R_FC32 *) pFFTSpec;
67
68   /* Validate args */
69   if (!pFFTSpec || (order < 1) || (order > TWIDDLE_TABLE_ORDER))
70     return OMX_Sts_BadArgErr;
71
72   /* Do the initializations */
73   Nby2 = 1 << (order - 1);
74   N = Nby2 << 1;
75
76   /* optimized implementations don't use bitreversal */
77   pBitRev = NULL;
78
79   pTwiddle = (OMX_FC32 *) (sizeof(ARMsFFTSpec_R_SC32) + (OMX_S8*) pFFTSpec);
80
81   /* Align to 32 byte boundary */
82   pTmp = ((uintptr_t)pTwiddle) & 31;
83   if (pTmp)
84     pTwiddle = (OMX_FC32*) ((OMX_S8*)pTwiddle + (32 - pTmp));
85
86   pBuf = (OMX_F32*) (sizeof(OMX_FC32)*(5*N/8) + (OMX_S8*) pTwiddle);
87
88   /* Align to 32 byte boundary */
89   pTmp = ((uintptr_t)pBuf)&31;                 /* (uintptr_t)pBuf % 32 */
90   if (pTmp)
91     pBuf = (OMX_F32*) ((OMX_S8*)pBuf + (32 - pTmp));
92
93   /*
94    * Filling Twiddle factors :
95    *
96    * exp^(-j*2*PI*k/ (N/2) ) ; k=0,1,2,...,3/4(N/2)
97    *
98    * N/2 point complex FFT is used to compute N point real FFT The
99    * original twiddle table "armSP_FFT_F32TwiddleTable" is of size
100    * (MaxSize/8 + 1) Rest of the values i.e., upto MaxSize are
101    * calculated using the symmetries of sin and cos The max size of
102    * the twiddle table needed is 3/4(N/2) for a radix-4 stage
103    *
104    * W = (-2 * PI) / N
105    * N = 1 << order
106    * W = -PI >> (order - 1)
107    */
108
109   M = Nby2 >> 3;
110   diff = TWIDDLE_TABLE_ORDER - (order - 1);
111   /* step into the twiddle table for the current order */
112   step = 1 << diff;
113
114   x = armSP_FFT_F32TwiddleTable[0];
115   y = armSP_FFT_F32TwiddleTable[1];
116   xNeg = 1;
117
118   if ((order - 1) >= 3) {
119     /* i = 0 case */
120     pTwiddle[0].Re = x;
121     pTwiddle[0].Im = y;
122     pTwiddle[2*M].Re = -y;
123     pTwiddle[2*M].Im = xNeg;
124     pTwiddle[4*M].Re = xNeg;
125     pTwiddle[4*M].Im = y;
126
127     for (i = 1; i <= M; i++) {
128       j = i*step;
129
130       x = armSP_FFT_F32TwiddleTable[2*j];
131       y = armSP_FFT_F32TwiddleTable[2*j+1];
132
133       pTwiddle[i].Re = x;
134       pTwiddle[i].Im = y;
135       pTwiddle[2*M-i].Re = -y;
136       pTwiddle[2*M-i].Im = -x;
137       pTwiddle[2*M+i].Re = y;
138       pTwiddle[2*M+i].Im = -x;
139       pTwiddle[4*M-i].Re = -x;
140       pTwiddle[4*M-i].Im = y;
141       pTwiddle[4*M+i].Re = -x;
142       pTwiddle[4*M+i].Im = -y;
143       pTwiddle[6*M-i].Re = y;
144       pTwiddle[6*M-i].Im = x;
145     }
146   } else if ((order - 1) == 2) {
147     pTwiddle[0].Re = x;
148     pTwiddle[0].Im = y;
149     pTwiddle[1].Re = -y;
150     pTwiddle[1].Im = xNeg;
151     pTwiddle[2].Re = xNeg;
152     pTwiddle[2].Im = y;
153   } else if ((order-1) == 1) {
154     pTwiddle[0].Re = x;
155     pTwiddle[0].Im = y;
156   }
157
158   /*
159    * Now fill the last N/4 values : exp^(-j*2*PI*k/N) ;
160    * k=1,3,5,...,N/2-1 These are used for the final twiddle fix-up for
161    * converting complex to real FFT
162    */
163
164   M = N >> 3;
165   diff = TWIDDLE_TABLE_ORDER - order;
166   step = 1 << diff;
167
168   pTwiddle1 = pTwiddle + 3*N/8;
169   pTwiddle4 = pTwiddle1 + (N/4 - 1);
170   pTwiddle3 = pTwiddle1 + N/8;
171   pTwiddle2 = pTwiddle1 + (N/8 - 1);
172
173   x = armSP_FFT_F32TwiddleTable[0];
174   y = armSP_FFT_F32TwiddleTable[1];
175   xNeg = 1;
176
177   if (order >=3) {
178     for (i = 1; i <= M; i += 2) {
179       j = i*step;
180
181       x = armSP_FFT_F32TwiddleTable[2*j];
182       y = armSP_FFT_F32TwiddleTable[2*j+1];
183
184       pTwiddle1[0].Re = x;
185       pTwiddle1[0].Im = y;
186       pTwiddle1 += 1;
187       pTwiddle2[0].Re = -y;
188       pTwiddle2[0].Im = -x;
189       pTwiddle2 -= 1;
190       pTwiddle3[0].Re = y;
191       pTwiddle3[0].Im = -x;
192       pTwiddle3 += 1;
193       pTwiddle4[0].Re = -x;
194       pTwiddle4[0].Im = y;
195       pTwiddle4 -= 1;
196     }
197   } else {
198     if (order == 2) {
199       pTwiddle1[0].Re = -y;
200       pTwiddle1[0].Im = xNeg;
201     }
202   }
203
204
205   /* Update the structure */
206   pFFTStruct->N = N;
207   pFFTStruct->pTwiddle = pTwiddle;
208   pFFTStruct->pBitRev = pBitRev;
209   pFFTStruct->pBuf = pBuf;
210
211   return OMX_Sts_NoErr;
212 }