FIRFilter.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// General FIR digital filter routines with MMX optimization.
  4. ///
  5. /// Notes : MMX optimized functions reside in a separate, platform-specific file,
  6. /// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
  7. ///
  8. /// This source file contains OpenMP optimizations that allow speeding up the
  9. /// corss-correlation algorithm by executing it in several threads / CPU cores
  10. /// in parallel. See the following article link for more detailed discussion
  11. /// about SoundTouch OpenMP optimizations:
  12. /// http://www.softwarecoven.com/parallel-computing-in-embedded-mobile-devices
  13. ///
  14. /// Author : Copyright (c) Olli Parviainen
  15. /// Author e-mail : oparviai 'at' iki.fi
  16. /// SoundTouch WWW: http://www.surina.net/soundtouch
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////
  19. //
  20. // License :
  21. //
  22. // SoundTouch audio processing library
  23. // Copyright (c) Olli Parviainen
  24. //
  25. // This library is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser General Public
  27. // License as published by the Free Software Foundation; either
  28. // version 2.1 of the License, or (at your option) any later version.
  29. //
  30. // This library is distributed in the hope that it will be useful,
  31. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. // Lesser General Public License for more details.
  34. //
  35. // You should have received a copy of the GNU Lesser General Public
  36. // License along with this library; if not, write to the Free Software
  37. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  38. //
  39. ////////////////////////////////////////////////////////////////////////////////
  40. #include <memory.h>
  41. #include <assert.h>
  42. #include <math.h>
  43. #include <stdlib.h>
  44. #include "FIRFilter.h"
  45. #include "cpu_detect.h"
  46. using namespace soundtouch;
  47. /*****************************************************************************
  48. *
  49. * Implementation of the class 'FIRFilter'
  50. *
  51. *****************************************************************************/
  52. FIRFilter::FIRFilter()
  53. {
  54. resultDivFactor = 0;
  55. resultDivider = 0;
  56. length = 0;
  57. lengthDiv8 = 0;
  58. filterCoeffs = nullptr;
  59. filterCoeffsStereo = nullptr;
  60. }
  61. FIRFilter::~FIRFilter()
  62. {
  63. delete[] filterCoeffs;
  64. delete[] filterCoeffsStereo;
  65. }
  66. // Usual C-version of the filter routine for stereo sound
  67. uint FIRFilter::evaluateFilterStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
  68. {
  69. int j, end;
  70. // hint compiler autovectorization that loop length is divisible by 8
  71. uint ilength = length & -8;
  72. assert((length != 0) && (length == ilength) && (src != nullptr) && (dest != nullptr) && (filterCoeffs != nullptr));
  73. assert(numSamples > ilength);
  74. end = 2 * (numSamples - ilength);
  75. #pragma omp parallel for
  76. for (j = 0; j < end; j += 2)
  77. {
  78. const SAMPLETYPE *ptr;
  79. LONG_SAMPLETYPE suml, sumr;
  80. suml = sumr = 0;
  81. ptr = src + j;
  82. for (uint i = 0; i < ilength; i ++)
  83. {
  84. suml += ptr[2 * i] * filterCoeffsStereo[2 * i];
  85. sumr += ptr[2 * i + 1] * filterCoeffsStereo[2 * i + 1];
  86. }
  87. #ifdef SOUNDTOUCH_INTEGER_SAMPLES
  88. suml >>= resultDivFactor;
  89. sumr >>= resultDivFactor;
  90. // saturate to 16 bit integer limits
  91. suml = (suml < -32768) ? -32768 : (suml > 32767) ? 32767 : suml;
  92. // saturate to 16 bit integer limits
  93. sumr = (sumr < -32768) ? -32768 : (sumr > 32767) ? 32767 : sumr;
  94. #endif // SOUNDTOUCH_INTEGER_SAMPLES
  95. dest[j] = (SAMPLETYPE)suml;
  96. dest[j + 1] = (SAMPLETYPE)sumr;
  97. }
  98. return numSamples - ilength;
  99. }
  100. // Usual C-version of the filter routine for mono sound
  101. uint FIRFilter::evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
  102. {
  103. int j, end;
  104. // hint compiler autovectorization that loop length is divisible by 8
  105. int ilength = length & -8;
  106. assert(ilength != 0);
  107. end = numSamples - ilength;
  108. #pragma omp parallel for
  109. for (j = 0; j < end; j ++)
  110. {
  111. const SAMPLETYPE *pSrc = src + j;
  112. LONG_SAMPLETYPE sum;
  113. int i;
  114. sum = 0;
  115. for (i = 0; i < ilength; i ++)
  116. {
  117. sum += pSrc[i] * filterCoeffs[i];
  118. }
  119. #ifdef SOUNDTOUCH_INTEGER_SAMPLES
  120. sum >>= resultDivFactor;
  121. // saturate to 16 bit integer limits
  122. sum = (sum < -32768) ? -32768 : (sum > 32767) ? 32767 : sum;
  123. #endif // SOUNDTOUCH_INTEGER_SAMPLES
  124. dest[j] = (SAMPLETYPE)sum;
  125. }
  126. return end;
  127. }
  128. uint FIRFilter::evaluateFilterMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels)
  129. {
  130. int j, end;
  131. assert(length != 0);
  132. assert(src != nullptr);
  133. assert(dest != nullptr);
  134. assert(filterCoeffs != nullptr);
  135. assert(numChannels < 16);
  136. // hint compiler autovectorization that loop length is divisible by 8
  137. int ilength = length & -8;
  138. end = numChannels * (numSamples - ilength);
  139. #pragma omp parallel for
  140. for (j = 0; j < end; j += numChannels)
  141. {
  142. const SAMPLETYPE *ptr;
  143. LONG_SAMPLETYPE sums[16];
  144. uint c;
  145. int i;
  146. for (c = 0; c < numChannels; c ++)
  147. {
  148. sums[c] = 0;
  149. }
  150. ptr = src + j;
  151. for (i = 0; i < ilength; i ++)
  152. {
  153. SAMPLETYPE coef=filterCoeffs[i];
  154. for (c = 0; c < numChannels; c ++)
  155. {
  156. sums[c] += ptr[0] * coef;
  157. ptr ++;
  158. }
  159. }
  160. for (c = 0; c < numChannels; c ++)
  161. {
  162. #ifdef SOUNDTOUCH_INTEGER_SAMPLES
  163. sums[c] >>= resultDivFactor;
  164. #endif // SOUNDTOUCH_INTEGER_SAMPLES
  165. dest[j+c] = (SAMPLETYPE)sums[c];
  166. }
  167. }
  168. return numSamples - ilength;
  169. }
  170. // Set filter coeffiecients and length.
  171. //
  172. // Throws an exception if filter length isn't divisible by 8
  173. void FIRFilter::setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor)
  174. {
  175. assert(newLength > 0);
  176. if (newLength % 8) ST_THROW_RT_ERROR("FIR filter length not divisible by 8");
  177. #ifdef SOUNDTOUCH_FLOAT_SAMPLES
  178. // scale coefficients already here if using floating samples
  179. double scale = 1.0 / resultDivider;
  180. #else
  181. short scale = 1;
  182. #endif
  183. lengthDiv8 = newLength / 8;
  184. length = lengthDiv8 * 8;
  185. assert(length == newLength);
  186. resultDivFactor = uResultDivFactor;
  187. resultDivider = (SAMPLETYPE)::pow(2.0, (int)resultDivFactor);
  188. delete[] filterCoeffs;
  189. filterCoeffs = new SAMPLETYPE[length];
  190. delete[] filterCoeffsStereo;
  191. filterCoeffsStereo = new SAMPLETYPE[length*2];
  192. for (uint i = 0; i < length; i ++)
  193. {
  194. filterCoeffs[i] = (SAMPLETYPE)(coeffs[i] * scale);
  195. // create also stereo set of filter coefficients: this allows compiler
  196. // to autovectorize filter evaluation much more efficiently
  197. filterCoeffsStereo[2 * i] = (SAMPLETYPE)(coeffs[i] * scale);
  198. filterCoeffsStereo[2 * i + 1] = (SAMPLETYPE)(coeffs[i] * scale);
  199. }
  200. }
  201. uint FIRFilter::getLength() const
  202. {
  203. return length;
  204. }
  205. // Applies the filter to the given sequence of samples.
  206. //
  207. // Note : The amount of outputted samples is by value of 'filter_length'
  208. // smaller than the amount of input samples.
  209. uint FIRFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels)
  210. {
  211. assert(length > 0);
  212. assert(lengthDiv8 * 8 == length);
  213. if (numSamples < length) return 0;
  214. #ifndef USE_MULTICH_ALWAYS
  215. if (numChannels == 1)
  216. {
  217. return evaluateFilterMono(dest, src, numSamples);
  218. }
  219. else if (numChannels == 2)
  220. {
  221. return evaluateFilterStereo(dest, src, numSamples);
  222. }
  223. else
  224. #endif // USE_MULTICH_ALWAYS
  225. {
  226. assert(numChannels > 0);
  227. return evaluateFilterMulti(dest, src, numSamples, numChannels);
  228. }
  229. }
  230. // Operator 'new' is overloaded so that it automatically creates a suitable instance
  231. // depending on if we've a MMX-capable CPU available or not.
  232. void * FIRFilter::operator new(size_t)
  233. {
  234. // Notice! don't use "new FIRFilter" directly, use "newInstance" to create a new instance instead!
  235. ST_THROW_RT_ERROR("Error in FIRFilter::new: Don't use 'new FIRFilter', use 'newInstance' member instead!");
  236. return newInstance();
  237. }
  238. FIRFilter * FIRFilter::newInstance()
  239. {
  240. uint uExtensions;
  241. uExtensions = detectCPUextensions();
  242. // Check if MMX/SSE instruction set extensions supported by CPU
  243. #ifdef SOUNDTOUCH_ALLOW_MMX
  244. // MMX routines available only with integer sample types
  245. if (uExtensions & SUPPORT_MMX)
  246. {
  247. return ::new FIRFilterMMX;
  248. }
  249. else
  250. #endif // SOUNDTOUCH_ALLOW_MMX
  251. #ifdef SOUNDTOUCH_ALLOW_SSE
  252. if (uExtensions & SUPPORT_SSE)
  253. {
  254. // SSE support
  255. return ::new FIRFilterSSE;
  256. }
  257. else
  258. #endif // SOUNDTOUCH_ALLOW_SSE
  259. {
  260. // ISA optimizations not supported, use plain C version
  261. return ::new FIRFilter;
  262. }
  263. }