Skip to content

Commit

Permalink
COMMON: Use ScopedPtr/ScopedArray in the Fourier Transform classes
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent af2ac29 commit eaca117
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 30 deletions.
8 changes: 3 additions & 5 deletions src/common/dct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@

namespace Common {

DCT::DCT(int bits, TransformType trans) : _bits(bits), _trans(trans), _rdft(0) {
DCT::DCT(int bits, TransformType trans) : _bits(bits), _trans(trans) {
int n = 1 << _bits;

_tCos = getCosineTable(_bits + 2);

_csc2 = new float[n / 2];
_csc2.reset(new float[n / 2]);

_rdft = new RDFT(_bits, (_trans == DCT_III) ? RDFT::IDFT_C2R : RDFT::DFT_R2C);
_rdft.reset(new RDFT(_bits, (_trans == DCT_III) ? RDFT::IDFT_C2R : RDFT::DFT_R2C));

for (int i = 0; i < (n / 2); i++)
_csc2[i] = 0.5 / sin((M_PI / (2 * n) * (2 * i + 1)));
}

DCT::~DCT() {
delete _rdft;
delete[] _csc2;
}

void DCT::calc(float *data) {
Expand Down
5 changes: 3 additions & 2 deletions src/common/dct.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <boost/noncopyable.hpp>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

namespace Common {

Expand All @@ -82,9 +83,9 @@ class DCT : boost::noncopyable {

const float *_tCos;

float *_csc2;
ScopedPtr<RDFT> _rdft;

RDFT *_rdft;
ScopedArray<float> _csc2;

void calcDCTI (float *data);
void calcDCTII (float *data);
Expand Down
13 changes: 5 additions & 8 deletions src/common/fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,19 @@ FFT::FFT(int bits, bool inverse) : _bits(bits), _inverse(inverse) {

int n = 1 << bits;

_tmpBuf = new Complex[n];
_expTab = new Complex[n / 2];
_revTab = new uint16[n];
_tmpBuf.reset(new Complex[n]);
_expTab.reset(new Complex[n / 2]);
_revTab.reset(new uint16[n]);

for (int i = 0; i < n; i++)
_revTab[-splitRadixPermutation(i, n, _inverse) & (n - 1)] = i;
}

FFT::~FFT() {
delete[] _revTab;
delete[] _expTab;
delete[] _tmpBuf;
}

const uint16 *FFT::getRevTab() const {
return _revTab;
return _revTab.get();
}

void FFT::permute(Complex *z) {
Expand All @@ -90,7 +87,7 @@ void FFT::permute(Complex *z) {
for (int j = 0; j < np; j++)
_tmpBuf[_revTab[j]] = z[j];

std::memcpy(z, _tmpBuf, np * sizeof(Complex));
std::memcpy(z, _tmpBuf.get(), np * sizeof(Complex));

return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/common/fft.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <boost/noncopyable.hpp>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

namespace Common {

Expand Down Expand Up @@ -83,10 +84,10 @@ class FFT : boost::noncopyable {
int _bits;
bool _inverse;

uint16 *_revTab;
ScopedArray<uint16> _revTab;

Complex *_expTab;
Complex *_tmpBuf;
ScopedArray<Complex> _expTab;
ScopedArray<Complex> _tmpBuf;

static int splitRadixPermutation(int i, int n, bool inverse);
};
Expand Down
10 changes: 4 additions & 6 deletions src/common/mdct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@

namespace Common {

MDCT::MDCT(int bits, bool inverse, double scale) : _bits(bits), _fft(0) {
MDCT::MDCT(int bits, bool inverse, double scale) : _bits(bits) {
_size = 1 << bits;

_fft = new FFT(_bits - 2, inverse);
_fft.reset(new FFT(_bits - 2, inverse));

const int size2 = _size >> 1;
const int size4 = _size >> 2;

_tCos = new float[size2];
_tSin = _tCos + size4;
_tCos.reset(new float[size2]);
_tSin = _tCos.get() + size4;

const double theta = 1.0 / 8.0 + (scale < 0 ? size4 : 0);

Expand All @@ -78,8 +78,6 @@ MDCT::MDCT(int bits, bool inverse, double scale) : _bits(bits), _fft(0) {
}

MDCT::~MDCT() {
delete[] _tCos;
delete _fft;
}

#define CMUL(dre, dim, are, aim, bre, bim) do { \
Expand Down
5 changes: 3 additions & 2 deletions src/common/mdct.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <boost/noncopyable.hpp>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

namespace Common {

Expand All @@ -75,10 +76,10 @@ class MDCT : boost::noncopyable {
int _bits;
int _size;

float *_tCos;
ScopedArray<float> _tCos;
float *_tSin;

FFT *_fft;
ScopedPtr<FFT> _fft;

/** Compute the middle half of the inverse MDCT of size N = 2^nbits,
* thus excluding the parts that can be derived by symmetry.
Expand Down
5 changes: 2 additions & 3 deletions src/common/rdft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@

namespace Common {

RDFT::RDFT(int bits, TransformType trans) : _bits(bits), _fft(0) {
RDFT::RDFT(int bits, TransformType trans) : _bits(bits) {
assert ((_bits >= 4) && (_bits <= 16));

_inverse = trans == IDFT_C2R || trans == DFT_C2R;
_signConvention = trans == IDFT_R2C || trans == DFT_C2R ? 1 : -1;

_fft = new FFT(bits - 1, trans == IDFT_C2R || trans == IDFT_R2C);
_fft.reset(new FFT(bits - 1, trans == IDFT_C2R || trans == IDFT_R2C));

int n = 1 << bits;

Expand All @@ -73,7 +73,6 @@ RDFT::RDFT(int bits, TransformType trans) : _bits(bits), _fft(0) {
}

RDFT::~RDFT() {
delete _fft;
}

void RDFT::calc(float *data) {
Expand Down
3 changes: 2 additions & 1 deletion src/common/rdft.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <boost/noncopyable.hpp>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

namespace Common {

Expand Down Expand Up @@ -82,7 +83,7 @@ class RDFT : boost::noncopyable {
const float *_tSin;
const float *_tCos;

FFT *_fft;
ScopedPtr<FFT> _fft;
};

} // End of namespace Common
Expand Down

0 comments on commit eaca117

Please sign in to comment.