1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
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:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 // FFTFrame implementation using FFmpeg's RDFT algorithm,
27 // suitable for use on Windows and Linux.
28
29 #include "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32
33 #if USE(WEBAUDIO_FFMPEG)
34
35 #include "platform/audio/FFTFrame.h"
36
37 #include "platform/audio/VectorMath.h"
38
39 extern "C" {
40 #include <libavcodec/avfft.h>
41 }
42
43 #include "wtf/MathExtras.h"
44
45 namespace {
46
47 struct FFTComplexProxy {
48 int16_t re;
49 int16_t im;
50 };
51
52 struct FFTContextProxy {
53 int nbits;
54 int inverse;
55 uint16_t* revtab;
56 FFTComplexProxy* tmpBuf;
57 int mdctSize;
58 int mdctBits;
59 void* tcos;
60 void* tsin;
61 void (*fftPermute)();
62 void (*fftCalc)();
63 void (*imdctCalc)();
64 void (*imdctHalf)();
65 void (*mdctCalc)();
66 void (*mdctCalcw)();
67 int fftPermutation;
68 int mdctPermutation;
69 };
70
71 struct RDFTContextProxy {
72 int nbits;
73 int inverse;
74 int signConvention;
75 const void* tcos;
76 const void* tsin;
77 FFTContextProxy fft;
78 void (*rdft_calc)();
79 };
80
81 }
82
83 namespace WebCore {
84
85 #if !ASSERT_DISABLED
86 const int kMaxFFTPow2Size = 24;
87 #endif
88
89 // Normal constructor: allocates for a given fftSize.
FFTFrame(unsigned fftSize)90 FFTFrame::FFTFrame(unsigned fftSize)
91 : m_FFTSize(fftSize)
92 , m_log2FFTSize(static_cast<unsigned>(log2(fftSize)))
93 , m_forwardContext(0)
94 , m_inverseContext(0)
95 , m_complexData(fftSize)
96 , m_realData(fftSize / 2)
97 , m_imagData(fftSize / 2)
98 {
99 // We only allow power of two.
100 ASSERT(1UL << m_log2FFTSize == m_FFTSize);
101
102 m_forwardContext = contextForSize(fftSize, DFT_R2C);
103 m_inverseContext = contextForSize(fftSize, IDFT_C2R);
104 }
105
106 // Creates a blank/empty frame (interpolate() must later be called).
FFTFrame()107 FFTFrame::FFTFrame()
108 : m_FFTSize(0)
109 , m_log2FFTSize(0)
110 , m_forwardContext(0)
111 , m_inverseContext(0)
112 {
113 }
114
115 // Copy constructor.
FFTFrame(const FFTFrame & frame)116 FFTFrame::FFTFrame(const FFTFrame& frame)
117 : m_FFTSize(frame.m_FFTSize)
118 , m_log2FFTSize(frame.m_log2FFTSize)
119 , m_forwardContext(0)
120 , m_inverseContext(0)
121 , m_complexData(frame.m_FFTSize)
122 , m_realData(frame.m_FFTSize / 2)
123 , m_imagData(frame.m_FFTSize / 2)
124 {
125 m_forwardContext = contextForSize(m_FFTSize, DFT_R2C);
126 m_inverseContext = contextForSize(m_FFTSize, IDFT_C2R);
127
128 // Copy/setup frame data.
129 unsigned nbytes = sizeof(float) * (m_FFTSize / 2);
130 memcpy(realData(), frame.realData(), nbytes);
131 memcpy(imagData(), frame.imagData(), nbytes);
132 }
133
initialize()134 void FFTFrame::initialize()
135 {
136 }
137
cleanup()138 void FFTFrame::cleanup()
139 {
140 }
141
~FFTFrame()142 FFTFrame::~FFTFrame()
143 {
144 av_rdft_end(m_forwardContext);
145 av_rdft_end(m_inverseContext);
146 }
147
multiply(const FFTFrame & frame)148 void FFTFrame::multiply(const FFTFrame& frame)
149 {
150 FFTFrame& frame1 = *this;
151 FFTFrame& frame2 = const_cast<FFTFrame&>(frame);
152
153 float* realP1 = frame1.realData();
154 float* imagP1 = frame1.imagData();
155 const float* realP2 = frame2.realData();
156 const float* imagP2 = frame2.imagData();
157
158 unsigned halfSize = fftSize() / 2;
159 float real0 = realP1[0];
160 float imag0 = imagP1[0];
161
162 VectorMath::zvmul(realP1, imagP1, realP2, imagP2, realP1, imagP1, halfSize);
163
164 // Multiply the packed DC/nyquist component
165 realP1[0] = real0 * realP2[0];
166 imagP1[0] = imag0 * imagP2[0];
167
168 // Scale accounts the peculiar scaling of vecLib on the Mac.
169 // This ensures the right scaling all the way back to inverse FFT.
170 // FIXME: if we change the scaling on the Mac then this scale
171 // factor will need to change too.
172 float scale = 0.5f;
173
174 VectorMath::vsmul(realP1, 1, &scale, realP1, 1, halfSize);
175 VectorMath::vsmul(imagP1, 1, &scale, imagP1, 1, halfSize);
176 }
177
doFFT(const float * data)178 void FFTFrame::doFFT(const float* data)
179 {
180 // Copy since processing is in-place.
181 float* p = m_complexData.data();
182 memcpy(p, data, sizeof(float) * m_FFTSize);
183
184 // Compute Forward transform.
185 av_rdft_calc(m_forwardContext, p);
186
187 // De-interleave to separate real and complex arrays.
188 int len = m_FFTSize / 2;
189
190 // FIXME: see above comment in multiply() about scaling.
191 const float scale = 2.0f;
192
193 for (int i = 0; i < len; ++i) {
194 int baseComplexIndex = 2 * i;
195 // m_realData[0] is the DC component and m_imagData[0] is the nyquist component
196 // since the interleaved complex data is packed.
197 m_realData[i] = scale * p[baseComplexIndex];
198 m_imagData[i] = scale * p[baseComplexIndex + 1];
199 }
200 }
201
doInverseFFT(float * data)202 void FFTFrame::doInverseFFT(float* data)
203 {
204 // Prepare interleaved data.
205 float* interleavedData = getUpToDateComplexData();
206
207 // Compute inverse transform.
208 av_rdft_calc(m_inverseContext, interleavedData);
209
210 // Scale so that a forward then inverse FFT yields exactly the original data.
211 const float scale = 1.0 / m_FFTSize;
212 VectorMath::vsmul(interleavedData, 1, &scale, data, 1, m_FFTSize);
213 }
214
realData() const215 float* FFTFrame::realData() const
216 {
217 return const_cast<float*>(m_realData.data());
218 }
219
imagData() const220 float* FFTFrame::imagData() const
221 {
222 return const_cast<float*>(m_imagData.data());
223 }
224
getUpToDateComplexData()225 float* FFTFrame::getUpToDateComplexData()
226 {
227 // FIXME: if we can't completely get rid of this method, SSE
228 // optimization could be considered if it shows up hot on profiles.
229 int len = m_FFTSize / 2;
230 for (int i = 0; i < len; ++i) {
231 int baseComplexIndex = 2 * i;
232 m_complexData[baseComplexIndex] = m_realData[i];
233 m_complexData[baseComplexIndex + 1] = m_imagData[i];
234 }
235 return const_cast<float*>(m_complexData.data());
236 }
237
contextForSize(unsigned fftSize,int trans)238 RDFTContext* FFTFrame::contextForSize(unsigned fftSize, int trans)
239 {
240 // FIXME: This is non-optimal. Ideally, we'd like to share the contexts for FFTFrames of the same size.
241 // But FFmpeg's RDFT uses a scratch buffer inside the context and so they are not thread-safe.
242 // We could improve this by sharing the FFTFrames on a per-thread basis.
243 ASSERT(fftSize);
244 int pow2size = static_cast<int>(log2(fftSize));
245 ASSERT(pow2size < kMaxFFTPow2Size);
246
247 RDFTContext* context = av_rdft_init(pow2size, (RDFTransformType)trans);
248 return context;
249 }
250
251 } // namespace WebCore
252
253 #endif // !OS(MACOSX) && USE(WEBAUDIO_FFMPEG)
254
255 #endif // ENABLE(WEB_AUDIO)
256