2
0

kbdwin.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avassert.h"
  19. #include "libavutil/mathematics.h"
  20. #include "libavutil/attributes.h"
  21. #include "kbdwin.h"
  22. #define BESSEL_I0_ITER 50 // default: 50 iterations of Bessel I0 approximation
  23. av_cold void ff_kbd_window_init(float *window, float alpha, int n)
  24. {
  25. int i, j;
  26. double sum = 0.0, bessel, tmp;
  27. double local_window[FF_KBD_WINDOW_MAX];
  28. double alpha2 = (alpha * M_PI / n) * (alpha * M_PI / n);
  29. av_assert0(n <= FF_KBD_WINDOW_MAX);
  30. for (i = 0; i < n; i++) {
  31. tmp = i * (n - i) * alpha2;
  32. bessel = 1.0;
  33. for (j = BESSEL_I0_ITER; j > 0; j--)
  34. bessel = bessel * tmp / (j * j) + 1;
  35. sum += bessel;
  36. local_window[i] = sum;
  37. }
  38. sum++;
  39. for (i = 0; i < n; i++)
  40. window[i] = sqrt(local_window[i] / sum);
  41. }
  42. av_cold void ff_kbd_window_init_fixed(int32_t *window, float alpha, int n)
  43. {
  44. int i;
  45. float local_window[FF_KBD_WINDOW_MAX];
  46. ff_kbd_window_init(local_window, alpha, n);
  47. for (i = 0; i < n; i++)
  48. window[i] = (int)floor(2147483647.0 * local_window[i] + 0.5);
  49. }