avcodec/kbdwin: Support arbitrary sized windows

Signed-off-by: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent 94d44dbe
......@@ -19,20 +19,23 @@
#include "libavutil/avassert.h"
#include "libavutil/mathematics.h"
#include "libavutil/attributes.h"
#include "libavutil/mem.h"
#include "kbdwin.h"
av_cold static void kbd_window_init(float *float_window, int *int_window, float alpha, int n)
av_cold static int kbd_window_init(float *float_window, int *int_window, float alpha, int n)
{
int i;
double sum = 0.0, tmp;
double scale = 0.0;
double temp[FF_KBD_WINDOW_MAX / 2 + 1];
double temp_small[FF_KBD_WINDOW_MAX / 2 + 1];
double *temp= n<=FF_KBD_WINDOW_MAX ? temp_small : av_malloc((n/2+1) * sizeof(*temp));
double alpha2 = 4 * (alpha * M_PI / n) * (alpha * M_PI / n);
av_assert0(n <= FF_KBD_WINDOW_MAX);
if (!temp)
return AVERROR(ENOMEM);
for (i = 0; i <= n / 2; i++) {
tmp = i * (n - i) * alpha2;
tmp = alpha2 * i * (n - i);
temp[i] = av_bessel_i0(sqrt(tmp));
scale += temp[i] * (1 + (i && i<n/2));
}
......@@ -48,14 +51,17 @@ av_cold static void kbd_window_init(float *float_window, int *int_window, float
if (float_window) float_window[i] = sqrt(sum * scale);
else int_window[i] = lrint(2147483647 * sqrt(sum * scale));
}
if (temp != temp_small)
av_free(temp);
return 0;
}
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
av_cold int ff_kbd_window_init(float *window, float alpha, int n)
{
kbd_window_init(window, NULL, alpha, n);
return kbd_window_init(window, NULL, alpha, n);
}
av_cold void ff_kbd_window_init_fixed(int32_t *window, float alpha, int n)
av_cold int ff_kbd_window_init_fixed(int32_t *window, float alpha, int n)
{
kbd_window_init(NULL, window, alpha, n);
return kbd_window_init(NULL, window, alpha, n);
}
......@@ -30,9 +30,11 @@
* Generate a Kaiser-Bessel Derived Window.
* @param window pointer to half window
* @param alpha determines window shape
* @param n size of half window, max FF_KBD_WINDOW_MAX
* @param n size of half window
*
* @return if n is larger than FF_KBD_WINDOW_MAX then AVERROR(ENOMEM) is possible
*/
void ff_kbd_window_init(float *window, float alpha, int n);
void ff_kbd_window_init_fixed(int32_t *window, float alpha, int n);
int ff_kbd_window_init(float *window, float alpha, int n);
int ff_kbd_window_init_fixed(int32_t *window, float alpha, int n);
#endif /* AVCODEC_KBDWIN_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment