You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.4KB

  1. /*
  2. * Copyright (c) 2015 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVFILTER_WINDOW_FUNC_H
  21. #define AVFILTER_WINDOW_FUNC_H
  22. #include <math.h>
  23. #include "libavutil/avassert.h"
  24. enum WindowFunc { WFUNC_RECT, WFUNC_HANNING, WFUNC_HAMMING, WFUNC_BLACKMAN,
  25. WFUNC_BARTLETT, WFUNC_WELCH, WFUNC_FLATTOP,
  26. WFUNC_BHARRIS, WFUNC_BNUTTALL, WFUNC_SINE, WFUNC_NUTTALL,
  27. WFUNC_BHANN, WFUNC_LANCZOS, WFUNC_GAUSS, WFUNC_TUKEY,
  28. WFUNC_DOLPH, WFUNC_CAUCHY, WFUNC_PARZEN, WFUNC_POISSON,
  29. NB_WFUNC };
  30. static inline void generate_window_func(float *lut, int N, int win_func,
  31. float *overlap)
  32. {
  33. int n;
  34. switch (win_func) {
  35. case WFUNC_RECT:
  36. for (n = 0; n < N; n++)
  37. lut[n] = 1.;
  38. *overlap = 0.;
  39. break;
  40. case WFUNC_BARTLETT:
  41. for (n = 0; n < N; n++)
  42. lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
  43. *overlap = 0.5;
  44. break;
  45. case WFUNC_HANNING:
  46. for (n = 0; n < N; n++)
  47. lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
  48. *overlap = 0.5;
  49. break;
  50. case WFUNC_HAMMING:
  51. for (n = 0; n < N; n++)
  52. lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
  53. *overlap = 0.5;
  54. break;
  55. case WFUNC_BLACKMAN:
  56. for (n = 0; n < N; n++)
  57. lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
  58. *overlap = 0.661;
  59. break;
  60. case WFUNC_WELCH:
  61. for (n = 0; n < N; n++)
  62. lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
  63. *overlap = 0.293;
  64. break;
  65. case WFUNC_FLATTOP:
  66. for (n = 0; n < N; n++)
  67. lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
  68. 1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
  69. 0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
  70. 0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
  71. 0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
  72. *overlap = 0.841;
  73. break;
  74. case WFUNC_BHARRIS:
  75. for (n = 0; n < N; n++)
  76. lut[n] = 0.35875-0.48829*cos(2*M_PI*n/(N-1))+0.14128*cos(4*M_PI*n/(N-1))-0.01168*cos(6*M_PI*n/(N-1));
  77. *overlap = 0.661;
  78. break;
  79. case WFUNC_BNUTTALL:
  80. for (n = 0; n < N; n++)
  81. lut[n] = 0.3635819-0.4891775*cos(2*M_PI*n/(N-1))+0.1365995*cos(4*M_PI*n/(N-1))-0.0106411*cos(6*M_PI*n/(N-1));
  82. *overlap = 0.661;
  83. break;
  84. case WFUNC_BHANN:
  85. for (n = 0; n < N; n++)
  86. lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
  87. *overlap = 0.5;
  88. break;
  89. case WFUNC_SINE:
  90. for (n = 0; n < N; n++)
  91. lut[n] = sin(M_PI*n/(N-1));
  92. *overlap = 0.75;
  93. break;
  94. case WFUNC_NUTTALL:
  95. for (n = 0; n < N; n++)
  96. lut[n] = 0.355768-0.487396*cos(2*M_PI*n/(N-1))+0.144232*cos(4*M_PI*n/(N-1))-0.012604*cos(6*M_PI*n/(N-1));
  97. *overlap = 0.663;
  98. break;
  99. case WFUNC_LANCZOS:
  100. #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
  101. for (n = 0; n < N; n++)
  102. lut[n] = SINC((2.*n)/(N-1)-1);
  103. *overlap = 0.75;
  104. break;
  105. case WFUNC_GAUSS:
  106. #define SQR(x) ((x)*(x))
  107. for (n = 0; n < N; n++)
  108. lut[n] = exp(-0.5 * SQR((n-(N-1)/2)/(0.4*(N-1)/2.f)));
  109. *overlap = 0.75;
  110. break;
  111. case WFUNC_TUKEY:
  112. for (n = 0; n < N; n++) {
  113. float M = (N-1)/2.;
  114. if (FFABS(n - M) >= 0.3 * M) {
  115. lut[n] = 0.5 * (1 + cos((M_PI*(FFABS(n - M) - 0.3 * M))/((1 - 0.3) * M)));
  116. } else {
  117. lut[n] = 1;
  118. }
  119. }
  120. *overlap = 0.33;
  121. break;
  122. case WFUNC_DOLPH: {
  123. double b = cosh(7.6009022095419887 / (N-1)), sum, t, c, norm = 0;
  124. int j;
  125. for (c = 1 - 1 / (b*b), n = (N-1) / 2; n >= 0; --n) {
  126. for (sum = !n, b = t = j = 1; j <= n && sum != t; b *= (n-j) * (1./j), ++j)
  127. t = sum, sum += (b *= c * (N - n - j) * (1./j));
  128. sum /= (N - 1 - n), sum /= (norm = norm ? norm : sum);
  129. lut[n] = sum;
  130. lut[N - 1 - n] = sum;
  131. }
  132. *overlap = 0.5;}
  133. break;
  134. case WFUNC_CAUCHY:
  135. for (n = 0; n < N; n++) {
  136. double x = 2 * ((n / (double)(N - 1)) - .5);
  137. if (x <= -.5 || x >= .5) {
  138. lut[n] = 0;
  139. } else {
  140. lut[n] = FFMIN(1, fabs(1/(1+4*16*x*x)));
  141. }
  142. }
  143. *overlap = 0.75;
  144. break;
  145. case WFUNC_PARZEN:
  146. for (n = 0; n < N; n++) {
  147. double x = 2 * ((n / (double)(N - 1)) - .5);
  148. if (x > 0.25 && x <= 0.5) {
  149. lut[n] = -2 * powf(-1 + 2 * x, 3);
  150. } else if (x >= -.5 && x < -.25) {
  151. lut[n] = 2 * powf(1 + 2 * x, 3);
  152. } else if (x >= -.25 && x < 0) {
  153. lut[n] = 1 - 24 * x * x - 48 * x * x * x;
  154. } else if (x >= 0 && x <= .25) {
  155. lut[n] = 1 - 24 * x * x + 48 * x * x * x;
  156. } else {
  157. lut[n] = 0;
  158. }
  159. }
  160. *overlap = 0.75;
  161. break;
  162. case WFUNC_POISSON:
  163. for (n = 0; n < N; n++) {
  164. double x = 2 * ((n / (double)(N - 1)) - .5);
  165. if (x >= 0 && x <= .5) {
  166. lut[n] = exp(-6*x);
  167. } else if (x < 0 && x >= -.5) {
  168. lut[n] = exp(6*x);
  169. } else {
  170. lut[n] = 0;
  171. }
  172. }
  173. *overlap = 0.75;
  174. break;
  175. default:
  176. av_assert0(0);
  177. }
  178. }
  179. #endif /* AVFILTER_WINDOW_FUNC_H */