1/*
2 * Copyright (C) 2010 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 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef FFTFrame_h
30#define FFTFrame_h
31
32#include "AudioArray.h"
33
34#if OS(DARWIN) && !USE(WEBAUDIO_FFMPEG)
35#define USE_ACCELERATE_FFT 1
36#else
37#define USE_ACCELERATE_FFT 0
38#endif
39
40#if USE_ACCELERATE_FFT
41#include <Accelerate/Accelerate.h>
42#endif
43
44#if !USE_ACCELERATE_FFT
45
46#if USE(WEBAUDIO_GSTREAMER)
47#include <glib.h>
48G_BEGIN_DECLS
49#include <gst/fft/gstfftf32.h>
50G_END_DECLS
51#endif // USE(WEBAUDIO_GSTREAMER)
52
53#if USE(WEBAUDIO_OPENMAX_DL_FFT)
54#include "dl/sp/api/armSP.h"
55#include "dl/sp/api/omxSP.h"
56#elif USE(WEBAUDIO_FFMPEG)
57struct RDFTContext;
58#endif
59
60#endif // !USE_ACCELERATE_FFT
61
62#if USE(WEBAUDIO_IPP)
63#include <ipps.h>
64#endif // USE(WEBAUDIO_IPP)
65
66#include <memory>
67#include <wtf/Forward.h>
68#include <wtf/Threading.h>
69
70namespace WebCore {
71
72// Defines the interface for an "FFT frame", an object which is able to perform a forward
73// and reverse FFT, internally storing the resultant frequency-domain data.
74
75class FFTFrame {
76public:
77    // The constructors, destructor, and methods up to the CROSS-PLATFORM section have platform-dependent implementations.
78
79    FFTFrame(unsigned fftSize);
80    FFTFrame(); // creates a blank/empty frame for later use with createInterpolatedFrame()
81    FFTFrame(const FFTFrame& frame);
82    ~FFTFrame();
83
84    static void initialize();
85    static void cleanup();
86    void doFFT(const float* data);
87    void doInverseFFT(float* data);
88    void multiply(const FFTFrame& frame); // multiplies ourself with frame : effectively operator*=()
89
90    float* realData() const;
91    float* imagData() const;
92
93    void print(); // for debugging
94
95    // CROSS-PLATFORM
96    // The remaining public methods have cross-platform implementations:
97
98    // Interpolates from frame1 -> frame2 as x goes from 0.0 -> 1.0
99    static std::unique_ptr<FFTFrame> createInterpolatedFrame(const FFTFrame& frame1, const FFTFrame& frame2, double x);
100
101    void doPaddedFFT(const float* data, size_t dataSize); // zero-padding with dataSize <= fftSize
102    double extractAverageGroupDelay();
103    void addConstantGroupDelay(double sampleFrameDelay);
104
105    unsigned fftSize() const { return m_FFTSize; }
106    unsigned log2FFTSize() const { return m_log2FFTSize; }
107
108private:
109    unsigned m_FFTSize;
110    unsigned m_log2FFTSize;
111
112    void interpolateFrequencyComponents(const FFTFrame& frame1, const FFTFrame& frame2, double x);
113
114#if USE_ACCELERATE_FFT
115    DSPSplitComplex& dspSplitComplex() { return m_frame; }
116    DSPSplitComplex dspSplitComplex() const { return m_frame; }
117
118    static FFTSetup fftSetupForSize(unsigned fftSize);
119
120    static FFTSetup* fftSetups;
121
122    FFTSetup m_FFTSetup;
123
124    DSPSplitComplex m_frame;
125    AudioFloatArray m_realData;
126    AudioFloatArray m_imagData;
127#else // !USE_ACCELERATE_FFT
128
129#if USE(WEBAUDIO_FFMPEG)
130    static RDFTContext* contextForSize(unsigned fftSize, int trans);
131
132    RDFTContext* m_forwardContext;
133    RDFTContext* m_inverseContext;
134
135    float* getUpToDateComplexData();
136    AudioFloatArray m_complexData;
137    AudioFloatArray m_realData;
138    AudioFloatArray m_imagData;
139#endif // USE(WEBAUDIO_FFMPEG)
140
141#if USE(WEBAUDIO_GSTREAMER)
142    GstFFTF32* m_fft;
143    GstFFTF32* m_inverseFft;
144    std::unique_ptr<GstFFTF32Complex[]> m_complexData;
145    AudioFloatArray m_realData;
146    AudioFloatArray m_imagData;
147#endif // USE(WEBAUDIO_GSTREAMER)
148
149#if USE(WEBAUDIO_IPP)
150    Ipp8u* m_buffer;
151    IppsDFTSpec_R_32f* m_DFTSpec;
152
153    float* getUpToDateComplexData();
154    AudioFloatArray m_complexData;
155    AudioFloatArray m_realData;
156    AudioFloatArray m_imagData;
157#endif // USE(WEBAUDIO_IPP)
158
159#if USE(WEBAUDIO_OPENMAX_DL_FFT)
160    static OMXFFTSpec_R_F32* contextForSize(unsigned log2FFTSize);
161
162    OMXFFTSpec_R_F32* m_forwardContext;
163    OMXFFTSpec_R_F32* m_inverseContext;
164
165    AudioFloatArray m_complexData;
166    AudioFloatArray m_realData;
167    AudioFloatArray m_imagData;
168#endif
169
170#endif // !USE_ACCELERATE_FFT
171};
172
173} // namespace WebCore
174
175#endif // FFTFrame_h
176