Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/FFTBlock.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef FFTBlock_h_
8
#define FFTBlock_h_
9
10
#ifdef BUILD_ARM_NEON
11
#include <cmath>
12
#include "mozilla/arm.h"
13
#include "dl/sp/api/omxSP.h"
14
#endif
15
16
#include "AlignedTArray.h"
17
#include "AudioNodeEngine.h"
18
#if defined(MOZ_LIBAV_FFT)
19
#include "FFmpegRDFTTypes.h"
20
#include "FFVPXRuntimeLinker.h"
21
#else
22
#include "kiss_fft/kiss_fftr.h"
23
#endif
24
25
namespace mozilla {
26
27
// This class defines an FFT block, loosely modeled after Blink's FFTFrame
28
// class to make sharing code with Blink easy.
29
// Currently it's implemented on top of KissFFT on all platforms.
30
class FFTBlock final
31
{
32
  union ComplexU {
33
#if !defined(MOZ_LIBAV_FFT)
34
    kiss_fft_cpx c;
35
#endif
36
    float f[2];
37
    struct {
38
      float r;
39
      float i;
40
    };
41
  };
42
43
public:
44
  static void MainThreadInit()
45
  {
46
#ifdef MOZ_LIBAV_FFT
47
    FFVPXRuntimeLinker::Init();
48
    FFVPXRuntimeLinker::GetRDFTFuncs(&sRDFTFuncs);
49
#endif
50
  }
51
52
  explicit FFTBlock(uint32_t aFFTSize)
53
#if defined(MOZ_LIBAV_FFT)
54
    : mAvRDFT(nullptr)
55
    , mAvIRDFT(nullptr)
56
#else
57
    : mKissFFT(nullptr)
58
    , mKissIFFT(nullptr)
59
#ifdef BUILD_ARM_NEON
60
    , mOmxFFT(nullptr)
61
    , mOmxIFFT(nullptr)
62
#endif
63
#endif
64
  {
65
    MOZ_COUNT_CTOR(FFTBlock);
66
    SetFFTSize(aFFTSize);
67
  }
68
  ~FFTBlock()
69
  {
70
    MOZ_COUNT_DTOR(FFTBlock);
71
    Clear();
72
  }
73
74
  // Return a new FFTBlock with frequency components interpolated between
75
  // |block0| and |block1| with |interp| between 0.0 and 1.0.
76
  static FFTBlock*
77
  CreateInterpolatedBlock(const FFTBlock& block0,
78
                          const FFTBlock& block1, double interp);
79
80
  // Transform FFTSize() points of aData and store the result internally.
81
  void PerformFFT(const float* aData)
82
  {
83
    if (!EnsureFFT()) {
84
      return;
85
    }
86
87
#if defined(MOZ_LIBAV_FFT)
88
    PodCopy(mOutputBuffer.Elements()->f, aData, mFFTSize);
89
    sRDFTFuncs.calc(mAvRDFT, mOutputBuffer.Elements()->f);
90
    // Recover packed Nyquist.
91
    mOutputBuffer[mFFTSize / 2].r = mOutputBuffer[0].i;
92
    mOutputBuffer[0].i = 0.0f;
93
#else
94
#ifdef BUILD_ARM_NEON
95
    if (mozilla::supports_neon()) {
96
      omxSP_FFTFwd_RToCCS_F32_Sfs(aData, mOutputBuffer.Elements()->f, mOmxFFT);
97
    } else
98
#endif
99
    {
100
      kiss_fftr(mKissFFT, aData, &(mOutputBuffer.Elements()->c));
101
    }
102
#endif
103
  }
104
  // Inverse-transform internal data and store the resulting FFTSize()
105
  // points in aDataOut.
106
  void GetInverse(float* aDataOut)
107
0
  {
108
0
    GetInverseWithoutScaling(aDataOut);
109
0
    AudioBufferInPlaceScale(aDataOut, 1.0f / mFFTSize, mFFTSize);
110
0
  }
111
112
  // Inverse-transform internal frequency data and store the resulting
113
  // FFTSize() points in |aDataOut|.  If frequency data has not already been
114
  // scaled, then the output will need scaling by 1/FFTSize().
115
  void GetInverseWithoutScaling(float* aDataOut)
116
  {
117
    if (!EnsureIFFT()) {
118
      std::fill_n(aDataOut, mFFTSize, 0.0f);
119
      return;
120
    };
121
122
#if defined(MOZ_LIBAV_FFT)
123
    {
124
      // Even though this function doesn't scale, the libav forward transform
125
      // gives a value that needs scaling by 2 in order for things to turn out
126
      // similar to how we expect from kissfft/openmax.
127
      AudioBufferCopyWithScale(mOutputBuffer.Elements()->f, 2.0f,
128
                               aDataOut, mFFTSize);
129
      aDataOut[1] = 2.0f * mOutputBuffer[mFFTSize/2].r; // Packed Nyquist
130
      sRDFTFuncs.calc(mAvIRDFT, aDataOut);
131
    }
132
#else
133
#ifdef BUILD_ARM_NEON
134
    if (mozilla::supports_neon()) {
135
      omxSP_FFTInv_CCSToR_F32_Sfs_unscaled(mOutputBuffer.Elements()->f, aDataOut, mOmxIFFT);
136
    } else
137
#endif
138
    {
139
      kiss_fftri(mKissIFFT, &(mOutputBuffer.Elements()->c), aDataOut);
140
    }
141
#endif
142
  }
143
144
  void Multiply(const FFTBlock& aFrame)
145
0
  {
146
0
    uint32_t halfSize = mFFTSize / 2;
147
0
    // DFTs are not packed.
148
0
    MOZ_ASSERT(mOutputBuffer[0].i == 0);
149
0
    MOZ_ASSERT(aFrame.mOutputBuffer[0].i == 0);
150
0
151
0
    BufferComplexMultiply(mOutputBuffer.Elements()->f,
152
0
                          aFrame.mOutputBuffer.Elements()->f,
153
0
                          mOutputBuffer.Elements()->f,
154
0
                          halfSize);
155
0
    mOutputBuffer[halfSize].r *= aFrame.mOutputBuffer[halfSize].r;
156
0
    // This would have been set to NaN if either real component was NaN.
157
0
    mOutputBuffer[0].i = 0.0f;
158
0
  }
159
160
  // Perform a forward FFT on |aData|, assuming zeros after dataSize samples,
161
  // and pre-scale the generated internal frequency domain coefficients so
162
  // that GetInverseWithoutScaling() can be used to transform to the time
163
  // domain.  This is useful for convolution kernels.
164
  void PadAndMakeScaledDFT(const float* aData, size_t dataSize)
165
0
  {
166
0
    MOZ_ASSERT(dataSize <= FFTSize());
167
0
    AlignedTArray<float> paddedData;
168
0
    paddedData.SetLength(FFTSize());
169
0
    AudioBufferCopyWithScale(aData, 1.0f / FFTSize(),
170
0
                             paddedData.Elements(), dataSize);
171
0
    PodZero(paddedData.Elements() + dataSize, mFFTSize - dataSize);
172
0
    PerformFFT(paddedData.Elements());
173
0
  }
174
175
  void SetFFTSize(uint32_t aSize)
176
  {
177
    mFFTSize = aSize;
178
    mOutputBuffer.SetLength(aSize / 2 + 1);
179
    PodZero(mOutputBuffer.Elements(), aSize / 2 + 1);
180
    Clear();
181
  }
182
183
  // Return the average group delay and removes this from the frequency data.
184
  double ExtractAverageGroupDelay();
185
186
  uint32_t FFTSize() const
187
  {
188
    return mFFTSize;
189
  }
190
  float RealData(uint32_t aIndex) const
191
  {
192
    return mOutputBuffer[aIndex].r;
193
  }
194
  float& RealData(uint32_t aIndex)
195
  {
196
    return mOutputBuffer[aIndex].r;
197
  }
198
  float ImagData(uint32_t aIndex) const
199
  {
200
    return mOutputBuffer[aIndex].i;
201
  }
202
  float& ImagData(uint32_t aIndex)
203
  {
204
    return mOutputBuffer[aIndex].i;
205
  }
206
207
  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
208
  {
209
    size_t amount = 0;
210
#if defined(MOZ_LIBAV_FFT)
211
    amount += aMallocSizeOf(mAvRDFT);
212
    amount += aMallocSizeOf(mAvIRDFT);
213
#else
214
    amount += aMallocSizeOf(mKissFFT);
215
    amount += aMallocSizeOf(mKissIFFT);
216
#endif
217
    amount += mOutputBuffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
218
    return amount;
219
  }
220
221
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
222
0
  {
223
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
224
0
  }
225
226
private:
227
  FFTBlock(const FFTBlock& other) = delete;
228
  void operator=(const FFTBlock& other) = delete;
229
230
  bool EnsureFFT()
231
  {
232
#if defined(MOZ_LIBAV_FFT)
233
    if (!mAvRDFT) {
234
      if (!sRDFTFuncs.init) {
235
        return false;
236
      }
237
238
      mAvRDFT = sRDFTFuncs.init(log((double)mFFTSize)/M_LN2, DFT_R2C);
239
    }
240
#else
241
#ifdef BUILD_ARM_NEON
242
    if (mozilla::supports_neon()) {
243
      if (!mOmxFFT) {
244
        mOmxFFT = createOmxFFT(mFFTSize);
245
      }
246
    } else
247
#endif
248
    {
249
      if (!mKissFFT) {
250
        mKissFFT = kiss_fftr_alloc(mFFTSize, 0, nullptr, nullptr);
251
      }
252
    }
253
#endif
254
    return true;
255
  }
256
257
  bool EnsureIFFT()
258
  {
259
#if defined(MOZ_LIBAV_FFT)
260
    if (!mAvIRDFT) {
261
      if (!sRDFTFuncs.init) {
262
        return false;
263
      }
264
265
      mAvIRDFT = sRDFTFuncs.init(log((double)mFFTSize)/M_LN2, IDFT_C2R);
266
    }
267
#else
268
#ifdef BUILD_ARM_NEON
269
    if (mozilla::supports_neon()) {
270
      if (!mOmxIFFT) {
271
        mOmxIFFT = createOmxFFT(mFFTSize);
272
      }
273
    } else
274
#endif
275
    {
276
      if (!mKissIFFT) {
277
        mKissIFFT = kiss_fftr_alloc(mFFTSize, 1, nullptr, nullptr);
278
      }
279
    }
280
#endif
281
    return true;
282
  }
283
284
#ifdef BUILD_ARM_NEON
285
  static OMXFFTSpec_R_F32* createOmxFFT(uint32_t aFFTSize)
286
  {
287
    MOZ_ASSERT((aFFTSize & (aFFTSize-1)) == 0);
288
    OMX_INT bufSize;
289
    OMX_INT order = log((double)aFFTSize)/M_LN2;
290
    MOZ_ASSERT(aFFTSize>>order == 1);
291
    OMXResult status = omxSP_FFTGetBufSize_R_F32(order, &bufSize);
292
    if (status == OMX_Sts_NoErr) {
293
      OMXFFTSpec_R_F32* context = static_cast<OMXFFTSpec_R_F32*>(malloc(bufSize));
294
      if (omxSP_FFTInit_R_F32(context, order) != OMX_Sts_NoErr) {
295
        return nullptr;
296
      }
297
      return context;
298
    }
299
    return nullptr;
300
  }
301
#endif
302
303
  void Clear()
304
  {
305
#if defined(MOZ_LIBAV_FFT)
306
    if (mAvRDFT) {
307
      sRDFTFuncs.end(mAvRDFT);
308
      mAvRDFT = nullptr;
309
    }
310
    if (mAvIRDFT) {
311
      sRDFTFuncs.end(mAvIRDFT);
312
      mAvIRDFT = nullptr;
313
    }
314
#else
315
#ifdef BUILD_ARM_NEON
316
    free(mOmxFFT);
317
    free(mOmxIFFT);
318
    mOmxFFT = mOmxIFFT = nullptr;
319
#endif
320
    free(mKissFFT);
321
    free(mKissIFFT);
322
    mKissFFT = mKissIFFT = nullptr;
323
#endif
324
  }
325
  void AddConstantGroupDelay(double sampleFrameDelay);
326
  void InterpolateFrequencyComponents(const FFTBlock& block0,
327
                                      const FFTBlock& block1, double interp);
328
#if defined(MOZ_LIBAV_FFT)
329
  static FFmpegRDFTFuncs sRDFTFuncs;
330
  RDFTContext *mAvRDFT;
331
  RDFTContext *mAvIRDFT;
332
#else
333
  kiss_fftr_cfg mKissFFT;
334
  kiss_fftr_cfg mKissIFFT;
335
#ifdef BUILD_ARM_NEON
336
  OMXFFTSpec_R_F32* mOmxFFT;
337
  OMXFFTSpec_R_F32* mOmxIFFT;
338
#endif
339
#endif
340
  AlignedTArray<ComplexU> mOutputBuffer;
341
  uint32_t mFFTSize;
342
};
343
344
} // namespace mozilla
345
346
#endif
347