2009-02-15 05:15:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
2013-04-17 10:46:57 +00:00
|
|
|
/// General FIR digital filter routines with MMX optimization.
|
2009-02-15 05:15:39 +00:00
|
|
|
///
|
2013-04-17 10:46:57 +00:00
|
|
|
/// Note : MMX optimized functions reside in a separate, platform-specific file,
|
2009-02-15 05:15:39 +00:00
|
|
|
/// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
|
|
|
|
///
|
|
|
|
/// Author : Copyright (c) Olli Parviainen
|
|
|
|
/// Author e-mail : oparviai 'at' iki.fi
|
|
|
|
/// SoundTouch WWW: http://www.surina.net/soundtouch
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2013-04-17 10:46:57 +00:00
|
|
|
// Last changed : $Date: 2011-09-02 15:56:11 -0300 (sex, 02 set 2011) $
|
2010-03-18 20:04:51 +00:00
|
|
|
// File revision : $Revision: 4 $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2013-04-17 10:46:57 +00:00
|
|
|
// $Id: FIRFilter.cpp 131 2011-09-02 18:56:11Z oparviai $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// License :
|
|
|
|
//
|
|
|
|
// SoundTouch audio processing library
|
|
|
|
// Copyright (c) Olli Parviainen
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <memory.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "FIRFilter.h"
|
|
|
|
#include "cpu_detect.h"
|
|
|
|
|
|
|
|
using namespace soundtouch;
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Implementation of the class 'FIRFilter'
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
FIRFilter::FIRFilter()
|
|
|
|
{
|
|
|
|
resultDivFactor = 0;
|
2010-03-18 20:04:51 +00:00
|
|
|
resultDivider = 0;
|
2009-02-15 05:15:39 +00:00
|
|
|
length = 0;
|
|
|
|
lengthDiv8 = 0;
|
|
|
|
filterCoeffs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FIRFilter::~FIRFilter()
|
|
|
|
{
|
|
|
|
delete[] filterCoeffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usual C-version of the filter routine for stereo sound
|
|
|
|
uint FIRFilter::evaluateFilterStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
|
|
|
|
{
|
|
|
|
uint i, j, end;
|
|
|
|
LONG_SAMPLETYPE suml, sumr;
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_FLOAT_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
// when using floating point samples, use a scaler instead of a divider
|
|
|
|
// because division is much slower operation than multiplying.
|
|
|
|
double dScaler = 1.0 / (double)resultDivider;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(length != 0);
|
2010-03-18 20:04:51 +00:00
|
|
|
assert(src != NULL);
|
|
|
|
assert(dest != NULL);
|
|
|
|
assert(filterCoeffs != NULL);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
end = 2 * (numSamples - length);
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
for (j = 0; j < end; j += 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
const SAMPLETYPE *ptr;
|
|
|
|
|
|
|
|
suml = sumr = 0;
|
|
|
|
ptr = src + j;
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
for (i = 0; i < length; i += 4)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// loop is unrolled by factor of 4 here for efficiency
|
|
|
|
suml += ptr[2 * i + 0] * filterCoeffs[i + 0] +
|
|
|
|
ptr[2 * i + 2] * filterCoeffs[i + 1] +
|
|
|
|
ptr[2 * i + 4] * filterCoeffs[i + 2] +
|
|
|
|
ptr[2 * i + 6] * filterCoeffs[i + 3];
|
|
|
|
sumr += ptr[2 * i + 1] * filterCoeffs[i + 0] +
|
|
|
|
ptr[2 * i + 3] * filterCoeffs[i + 1] +
|
|
|
|
ptr[2 * i + 5] * filterCoeffs[i + 2] +
|
|
|
|
ptr[2 * i + 7] * filterCoeffs[i + 3];
|
|
|
|
}
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_INTEGER_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
suml >>= resultDivFactor;
|
|
|
|
sumr >>= resultDivFactor;
|
|
|
|
// saturate to 16 bit integer limits
|
|
|
|
suml = (suml < -32768) ? -32768 : (suml > 32767) ? 32767 : suml;
|
|
|
|
// saturate to 16 bit integer limits
|
|
|
|
sumr = (sumr < -32768) ? -32768 : (sumr > 32767) ? 32767 : sumr;
|
|
|
|
#else
|
|
|
|
suml *= dScaler;
|
|
|
|
sumr *= dScaler;
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_INTEGER_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
dest[j] = (SAMPLETYPE)suml;
|
|
|
|
dest[j + 1] = (SAMPLETYPE)sumr;
|
|
|
|
}
|
|
|
|
return numSamples - length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Usual C-version of the filter routine for mono sound
|
|
|
|
uint FIRFilter::evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
|
|
|
|
{
|
|
|
|
uint i, j, end;
|
|
|
|
LONG_SAMPLETYPE sum;
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_FLOAT_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
// when using floating point samples, use a scaler instead of a divider
|
|
|
|
// because division is much slower operation than multiplying.
|
|
|
|
double dScaler = 1.0 / (double)resultDivider;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
assert(length != 0);
|
|
|
|
|
|
|
|
end = numSamples - length;
|
2013-04-17 10:46:57 +00:00
|
|
|
for (j = 0; j < end; j ++)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
sum = 0;
|
2013-04-17 10:46:57 +00:00
|
|
|
for (i = 0; i < length; i += 4)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
// loop is unrolled by factor of 4 here for efficiency
|
2013-04-17 10:46:57 +00:00
|
|
|
sum += src[i + 0] * filterCoeffs[i + 0] +
|
|
|
|
src[i + 1] * filterCoeffs[i + 1] +
|
|
|
|
src[i + 2] * filterCoeffs[i + 2] +
|
2009-02-15 05:15:39 +00:00
|
|
|
src[i + 3] * filterCoeffs[i + 3];
|
|
|
|
}
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_INTEGER_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
sum >>= resultDivFactor;
|
|
|
|
// saturate to 16 bit integer limits
|
|
|
|
sum = (sum < -32768) ? -32768 : (sum > 32767) ? 32767 : sum;
|
|
|
|
#else
|
|
|
|
sum *= dScaler;
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_INTEGER_SAMPLES
|
2009-02-15 05:15:39 +00:00
|
|
|
dest[j] = (SAMPLETYPE)sum;
|
|
|
|
src ++;
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set filter coeffiecients and length.
|
|
|
|
//
|
|
|
|
// Throws an exception if filter length isn't divisible by 8
|
|
|
|
void FIRFilter::setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor)
|
|
|
|
{
|
|
|
|
assert(newLength > 0);
|
2013-04-17 10:46:57 +00:00
|
|
|
if (newLength % 8) ST_THROW_RT_ERROR("FIR filter length not divisible by 8");
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
lengthDiv8 = newLength / 8;
|
|
|
|
length = lengthDiv8 * 8;
|
|
|
|
assert(length == newLength);
|
|
|
|
|
|
|
|
resultDivFactor = uResultDivFactor;
|
2010-03-18 20:04:51 +00:00
|
|
|
resultDivider = (SAMPLETYPE)::pow(2.0, (int)resultDivFactor);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
delete[] filterCoeffs;
|
|
|
|
filterCoeffs = new SAMPLETYPE[length];
|
|
|
|
memcpy(filterCoeffs, coeffs, length * sizeof(SAMPLETYPE));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint FIRFilter::getLength() const
|
|
|
|
{
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Applies the filter to the given sequence of samples.
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2013-04-17 10:46:57 +00:00
|
|
|
// Note : The amount of outputted samples is by value of 'filter_length'
|
2009-02-15 05:15:39 +00:00
|
|
|
// smaller than the amount of input samples.
|
|
|
|
uint FIRFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) const
|
|
|
|
{
|
|
|
|
assert(numChannels == 1 || numChannels == 2);
|
|
|
|
|
|
|
|
assert(length > 0);
|
|
|
|
assert(lengthDiv8 * 8 == length);
|
|
|
|
if (numSamples < length) return 0;
|
2013-04-17 10:46:57 +00:00
|
|
|
if (numChannels == 2)
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
return evaluateFilterStereo(dest, src, numSamples);
|
|
|
|
} else {
|
|
|
|
return evaluateFilterMono(dest, src, numSamples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Operator 'new' is overloaded so that it automatically creates a suitable instance
|
2009-02-15 05:15:39 +00:00
|
|
|
// depending on if we've a MMX-capable CPU available or not.
|
|
|
|
void * FIRFilter::operator new(size_t s)
|
|
|
|
{
|
|
|
|
// Notice! don't use "new FIRFilter" directly, use "newInstance" to create a new instance instead!
|
2013-04-17 10:46:57 +00:00
|
|
|
ST_THROW_RT_ERROR("Error in FIRFilter::new: Don't use 'new FIRFilter', use 'newInstance' member instead!");
|
|
|
|
return newInstance();
|
2009-02-15 05:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FIRFilter * FIRFilter::newInstance()
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
uint uExtensions;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
uExtensions = detectCPUextensions();
|
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
// Check if MMX/SSE instruction set extensions supported by CPU
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_ALLOW_MMX
|
2009-02-15 05:15:39 +00:00
|
|
|
// MMX routines available only with integer sample types
|
|
|
|
if (uExtensions & SUPPORT_MMX)
|
|
|
|
{
|
|
|
|
return ::new FIRFilterMMX;
|
|
|
|
}
|
|
|
|
else
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_ALLOW_MMX
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2013-04-17 10:46:57 +00:00
|
|
|
#ifdef SOUNDTOUCH_ALLOW_SSE
|
2009-02-15 05:15:39 +00:00
|
|
|
if (uExtensions & SUPPORT_SSE)
|
|
|
|
{
|
|
|
|
// SSE support
|
|
|
|
return ::new FIRFilterSSE;
|
|
|
|
}
|
|
|
|
else
|
2013-04-17 10:46:57 +00:00
|
|
|
#endif // SOUNDTOUCH_ALLOW_SSE
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// ISA optimizations not supported, use plain C version
|
|
|
|
return ::new FIRFilter;
|
|
|
|
}
|
|
|
|
}
|