diff mbox series

[FFmpeg-devel,3/5] avcodec/kbdwin: Support arbitrary sized windows

Message ID 20230616222048.6562-4-michael@niedermayer.cc
State Accepted
Commit cf00f60bab1f79213c274a6cd4357b32bd5c0101
Headers show
Series add sdr support | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Michael Niedermayer June 16, 2023, 10:20 p.m. UTC
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/kbdwin.c | 22 ++++++++++++++--------
 libavcodec/kbdwin.h |  8 +++++---
 2 files changed, 19 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/kbdwin.c b/libavcodec/kbdwin.c
index 163f9d5251..82755874d4 100644
--- a/libavcodec/kbdwin.c
+++ b/libavcodec/kbdwin.c
@@ -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);
 }
diff --git a/libavcodec/kbdwin.h b/libavcodec/kbdwin.h
index 4185c4206f..452fc46596 100644
--- a/libavcodec/kbdwin.h
+++ b/libavcodec/kbdwin.h
@@ -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 */