DPL2Decoder: Prefer std::vector to calloc-ed array

This commit is contained in:
MerryMage 2017-04-22 10:01:26 +01:00
parent 71dc810418
commit 42a1f7939d
1 changed files with 27 additions and 32 deletions

View File

@ -10,8 +10,8 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include <functional> #include <functional>
#include <string.h>
#include <vector> #include <vector>
#include "AudioCommon/DPL2Decoder.h" #include "AudioCommon/DPL2Decoder.h"
@ -35,11 +35,11 @@ static float adapt_l_gain, adapt_r_gain, adapt_lpr_gain, adapt_lmr_gain;
static std::vector<float> lf, rf, lr, rr, cf, cr; static std::vector<float> lf, rf, lr, rr, cf, cr;
static float LFE_buf[256]; static float LFE_buf[256];
static unsigned int lfe_pos; static unsigned int lfe_pos;
static float* filter_coefs_lfe; static std::vector<float> filter_coefs_lfe;
static unsigned int len125; static unsigned int len125;
template <class T, class _ftype_t> template <class T>
static _ftype_t DotProduct(int count, const T* buf, const _ftype_t* coefficients) static float DotProduct(int count, const T* buf, const std::vector<float>& coeffs, int offset)
{ {
int i; int i;
float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f; float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f;
@ -47,21 +47,21 @@ static _ftype_t DotProduct(int count, const T* buf, const _ftype_t* coefficients
// Unrolled loop // Unrolled loop
for (i = 0; (i + 3) < count; i += 4) for (i = 0; (i + 3) < count; i += 4)
{ {
sum0 += buf[i + 0] * coefficients[i + 0]; sum0 += buf[i + 0] * coeffs[offset + i + 0];
sum1 += buf[i + 1] * coefficients[i + 1]; sum1 += buf[i + 1] * coeffs[offset + i + 1];
sum2 += buf[i + 2] * coefficients[i + 2]; sum2 += buf[i + 2] * coeffs[offset + i + 2];
sum3 += buf[i + 3] * coefficients[i + 3]; sum3 += buf[i + 3] * coeffs[offset + i + 3];
} }
// Epilogue of unrolled loop // Epilogue of unrolled loop
for (; i < count; i++) for (; i < count; i++)
sum0 += buf[i] * coefficients[i]; sum0 += buf[i] * coeffs[offset + i];
return sum0 + sum1 + sum2 + sum3; return sum0 + sum1 + sum2 + sum3;
} }
template <class T> template <class T>
static T FIRFilter(const T* buf, int pos, int len, int count, const float* coefficients) static T FIRFilter(const T* buf, int pos, int len, int count, const std::vector<float>& coeffs)
{ {
int count1, count2; int count1, count2;
@ -81,9 +81,8 @@ static T FIRFilter(const T* buf, int pos, int len, int count, const float* coeff
// high part of window // high part of window
const T* ptr = &buf[pos]; const T* ptr = &buf[pos];
float r1 = DotProduct(count1, ptr, coefficients); float r1 = DotProduct(count1, ptr, coeffs, 0);
coefficients += count1; float r2 = DotProduct(count2, buf, coeffs, count1);
float r2 = DotProduct(count2, buf, coefficients);
return T(r1 + r2); return T(r1 + r2);
} }
@ -94,15 +93,19 @@ static T FIRFilter(const T* buf, int pos, int len, int count, const float* coeff
// N-1 // N-1
// //
// n window length // n window length
// w buffer for the window parameters // returns buffer with the window parameters
*/ */
static void Hamming(int n, float* w) static std::vector<float> Hamming(int n)
{ {
std::vector<float> w(n);
float k = float(2 * M_PI / ((float)(n - 1))); // 2*pi/(N-1) float k = float(2 * M_PI / ((float)(n - 1))); // 2*pi/(N-1)
// Calculate window coefficients // Calculate window coefficients
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
*w++ = float(0.54 - 0.46 * cos(k * (float)i)); w[i] = float(0.54 - 0.46 * cos(k * (float)i));
return w;
} }
// FIR filter design // FIR filter design
@ -110,7 +113,6 @@ static void Hamming(int n, float* w)
/* Design FIR filter using the Window method /* Design FIR filter using the Window method
n filter length must be odd for HP and BS filters n filter length must be odd for HP and BS filters
w buffer for the filter taps (must be n long)
fc cutoff frequencies (1 for LP and HP, 2 for BP and BS) fc cutoff frequencies (1 for LP and HP, 2 for BP and BS)
0 < fc < 1 where 1 <=> Fs/2 0 < fc < 1 where 1 <=> Fs/2
flags window and filter type as defined in filter.h flags window and filter type as defined in filter.h
@ -118,9 +120,9 @@ variables are ored together: i.e. LP|HAMMING will give a
low pass filter designed using a hamming window low pass filter designed using a hamming window
opt beta constant used only when designing using kaiser windows opt beta constant used only when designing using kaiser windows
returns 0 if OK, -1 if fail returns buffer for the filter taps (will be n long)
*/ */
static float* DesignFIR(unsigned int n, float fc, float opt) static std::vector<float> DesignFIR(unsigned int n, float fc, float opt)
{ {
const unsigned int o = n & 1; // Indicator for odd filter length const unsigned int o = n & 1; // Indicator for odd filter length
const unsigned int end = ((n + 1) >> 1) - o; // Loop end const unsigned int end = ((n + 1) >> 1) - o; // Loop end
@ -134,12 +136,10 @@ static float* DesignFIR(unsigned int n, float fc, float opt)
// Sanity check // Sanity check
if (n == 0) if (n == 0)
return nullptr; return {};
float* w = (float*)calloc(sizeof(float), n);
// Get window coefficients // Get window coefficients
Hamming(n, w); std::vector<float> w = Hamming(n);
// Low pass filter // Low pass filter
@ -190,19 +190,14 @@ static void Done()
{ {
OnSeek(); OnSeek();
if (filter_coefs_lfe) filter_coefs_lfe.clear();
{
free(filter_coefs_lfe);
} }
filter_coefs_lfe = nullptr; static std::vector<float> CalculateCoefficients125HzLowpass(int rate)
}
static float* CalculateCoefficients125HzLowpass(int rate)
{ {
len125 = 256; len125 = 256;
float f = 125.0f / (rate / 2); float f = 125.0f / (rate / 2);
float* coeffs = DesignFIR(len125, f, 0); std::vector<float> coeffs = DesignFIR(len125, f, 0);
static const float M3_01DB = 0.7071067812f; static const float M3_01DB = 0.7071067812f;
for (unsigned int i = 0; i < len125; i++) for (unsigned int i = 0; i < len125; i++)
{ {
@ -380,5 +375,5 @@ void DPL2Reset()
{ {
olddelay = -1; olddelay = -1;
oldfreq = 0; oldfreq = 0;
filter_coefs_lfe = nullptr; filter_coefs_lfe.clear();
} }