diff mbox series

[FFmpeg-devel,5/6] avcodec/kbdwin: Avoid computing bessel values twice

Message ID 20230522233601.20552-5-michael@niedermayer.cc
State Accepted
Commit 17ecb224e94a3c111017843afbc255303ed01474
Headers show
Series [FFmpeg-devel,1/6] Move bessel_i0() from swresample/resample to avutil/mathematics | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Michael Niedermayer May 22, 2023, 11:36 p.m. UTC
Also reduce neeeded temporary storage by half

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/kbdwin.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/kbdwin.c b/libavcodec/kbdwin.c
index 5dff334250..e2f6491124 100644
--- a/libavcodec/kbdwin.c
+++ b/libavcodec/kbdwin.c
@@ -23,23 +23,29 @@ 
 
 av_cold void ff_kbd_window_init(float *window, float alpha, int n)
 {
-   int i, j;
-   double sum = 0.0, bessel, tmp;
-   double local_window[FF_KBD_WINDOW_MAX];
-   double alpha2 = (alpha * M_PI / n) * (alpha * M_PI / n);
+   int i;
+   double sum = 0.0, tmp;
+   double scale = 0.0;
+   double temp[FF_KBD_WINDOW_MAX / 2 + 1];
+   double alpha2 = 4 * (alpha * M_PI / n) * (alpha * M_PI / n);
 
    av_assert0(n <= FF_KBD_WINDOW_MAX);
 
-   for (i = 0; i < n; i++) {
+   for (i = 0; i <= n / 2; i++) {
        tmp = i * (n - i) * alpha2;
-       bessel = av_bessel_i0(sqrt(tmp) * 2);
-       sum += bessel;
-       local_window[i] = sum;
+       temp[i] = av_bessel_i0(sqrt(tmp));
+       scale += temp[i] * (1 + (i && i<n/2));
    }
+   scale = 1.0/(scale + 1);
 
-   sum++;
-   for (i = 0; i < n; i++)
-       window[i] = sqrt(local_window[i] / sum);
+   for (i = 0; i <= n / 2; i++) {
+       sum += temp[i];
+       window[i] = sqrt(sum * scale);
+   }
+   for (; i < n; i++) {
+       sum += temp[n - i];
+       window[i] = sqrt(sum * scale);
+   }
 }
 
 av_cold void ff_kbd_window_init_fixed(int32_t *window, float alpha, int n)