xref: /aosp_15_r20/external/webrtc/common_audio/real_fourier.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef COMMON_AUDIO_REAL_FOURIER_H_
12*d9f75844SAndroid Build Coastguard Worker #define COMMON_AUDIO_REAL_FOURIER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include <complex>
17*d9f75844SAndroid Build Coastguard Worker #include <memory>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/memory/aligned_malloc.h"
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker // Uniform interface class for the real DFT and its inverse, for power-of-2
22*d9f75844SAndroid Build Coastguard Worker // input lengths. Also contains helper functions for buffer allocation, taking
23*d9f75844SAndroid Build Coastguard Worker // care of any memory alignment requirements the underlying library might have.
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker class RealFourier {
28*d9f75844SAndroid Build Coastguard Worker  public:
29*d9f75844SAndroid Build Coastguard Worker   // Shorthand typenames for the scopers used by the buffer allocation helpers.
30*d9f75844SAndroid Build Coastguard Worker   typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper;
31*d9f75844SAndroid Build Coastguard Worker   typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter>
32*d9f75844SAndroid Build Coastguard Worker       fft_cplx_scoper;
33*d9f75844SAndroid Build Coastguard Worker 
34*d9f75844SAndroid Build Coastguard Worker   // The alignment required for all input and output buffers, in bytes.
35*d9f75844SAndroid Build Coastguard Worker   static const size_t kFftBufferAlignment;
36*d9f75844SAndroid Build Coastguard Worker 
37*d9f75844SAndroid Build Coastguard Worker   // Construct a wrapper instance for the given input order, which must be
38*d9f75844SAndroid Build Coastguard Worker   // between 1 and kMaxFftOrder, inclusively.
39*d9f75844SAndroid Build Coastguard Worker   static std::unique_ptr<RealFourier> Create(int fft_order);
~RealFourier()40*d9f75844SAndroid Build Coastguard Worker   virtual ~RealFourier() {}
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   // Helper to compute the smallest FFT order (a power of 2) which will contain
43*d9f75844SAndroid Build Coastguard Worker   // the given input length.
44*d9f75844SAndroid Build Coastguard Worker   static int FftOrder(size_t length);
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker   // Helper to compute the input length from the FFT order.
47*d9f75844SAndroid Build Coastguard Worker   static size_t FftLength(int order);
48*d9f75844SAndroid Build Coastguard Worker 
49*d9f75844SAndroid Build Coastguard Worker   // Helper to compute the exact length, in complex floats, of the transform
50*d9f75844SAndroid Build Coastguard Worker   // output (i.e. |2^order / 2 + 1|).
51*d9f75844SAndroid Build Coastguard Worker   static size_t ComplexLength(int order);
52*d9f75844SAndroid Build Coastguard Worker 
53*d9f75844SAndroid Build Coastguard Worker   // Buffer allocation helpers. The buffers are large enough to hold `count`
54*d9f75844SAndroid Build Coastguard Worker   // floats/complexes and suitably aligned for use by the implementation.
55*d9f75844SAndroid Build Coastguard Worker   // The returned scopers are set up with proper deleters; the caller owns
56*d9f75844SAndroid Build Coastguard Worker   // the allocated memory.
57*d9f75844SAndroid Build Coastguard Worker   static fft_real_scoper AllocRealBuffer(int count);
58*d9f75844SAndroid Build Coastguard Worker   static fft_cplx_scoper AllocCplxBuffer(int count);
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker   // Main forward transform interface. The output array need only be big
61*d9f75844SAndroid Build Coastguard Worker   // enough for |2^order / 2 + 1| elements - the conjugate pairs are not
62*d9f75844SAndroid Build Coastguard Worker   // returned. Input and output must be properly aligned (e.g. through
63*d9f75844SAndroid Build Coastguard Worker   // AllocRealBuffer and AllocCplxBuffer) and input length must be
64*d9f75844SAndroid Build Coastguard Worker   // |2^order| (same as given at construction time).
65*d9f75844SAndroid Build Coastguard Worker   virtual void Forward(const float* src, std::complex<float>* dest) const = 0;
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker   // Inverse transform. Same input format as output above, conjugate pairs
68*d9f75844SAndroid Build Coastguard Worker   // not needed.
69*d9f75844SAndroid Build Coastguard Worker   virtual void Inverse(const std::complex<float>* src, float* dest) const = 0;
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker   virtual int order() const = 0;
72*d9f75844SAndroid Build Coastguard Worker };
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker #endif  // COMMON_AUDIO_REAL_FOURIER_H_
77