diff mbox series

[FFmpeg-devel,167/191] avcodec/sinewin_tablegen: Make initializing ff_sine_windows thread-safe

Message ID 20201123193739.1249300-5-andreas.rheinhardt@gmail.com
State Accepted
Commit 3719122065863f701026632f610175980d42b05a
Headers show
Series VLC, esp. init_vlc patches | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Nov. 23, 2020, 7:37 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/sinewin_tablegen.h          | 43 ++++++++++++++++++++++++++
 libavcodec/sinewin_tablegen_template.c |  1 +
 2 files changed, 44 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/sinewin_tablegen.h b/libavcodec/sinewin_tablegen.h
index 1959074189..ced3b7874c 100644
--- a/libavcodec/sinewin_tablegen.h
+++ b/libavcodec/sinewin_tablegen.h
@@ -32,6 +32,10 @@ 
 #include "libavutil/common.h"
 
 #if !CONFIG_HARDCODED_TABLES
+#ifndef BUILD_TABLES
+#include "libavutil/thread.h"
+#endif
+
 SINETABLE(  32);
 SINETABLE(  64);
 SINETABLE( 128);
@@ -69,10 +73,49 @@  av_cold void AAC_RENAME(ff_sine_window_init)(INTFLOAT *window, int n) {
         window[i] = SIN_FIX(sinf((i + 0.5) * (M_PI / (2.0 * n))));
 }
 
+#if !CONFIG_HARDCODED_TABLES && !defined(BUILD_TABLES)
+#define INIT_FF_SINE_WINDOW_INIT_FUNC(index)        \
+static void init_ff_sine_window_ ## index(void)     \
+{                                                   \
+    AAC_RENAME(ff_sine_window_init)(AAC_RENAME(ff_sine_windows)[index], 1 << index);\
+}
+
+INIT_FF_SINE_WINDOW_INIT_FUNC(5)
+INIT_FF_SINE_WINDOW_INIT_FUNC(6)
+INIT_FF_SINE_WINDOW_INIT_FUNC(7)
+INIT_FF_SINE_WINDOW_INIT_FUNC(8)
+INIT_FF_SINE_WINDOW_INIT_FUNC(9)
+INIT_FF_SINE_WINDOW_INIT_FUNC(10)
+INIT_FF_SINE_WINDOW_INIT_FUNC(11)
+INIT_FF_SINE_WINDOW_INIT_FUNC(12)
+INIT_FF_SINE_WINDOW_INIT_FUNC(13)
+
+static void (*const sine_window_init_func_array[])(void) = {
+    init_ff_sine_window_5,
+    init_ff_sine_window_6,
+    init_ff_sine_window_7,
+    init_ff_sine_window_8,
+    init_ff_sine_window_9,
+    init_ff_sine_window_10,
+    init_ff_sine_window_11,
+    init_ff_sine_window_12,
+    init_ff_sine_window_13,
+};
+
+static AVOnce init_sine_window_once[9] = {
+    AV_ONCE_INIT, AV_ONCE_INIT, AV_ONCE_INIT, AV_ONCE_INIT, AV_ONCE_INIT,
+    AV_ONCE_INIT, AV_ONCE_INIT, AV_ONCE_INIT, AV_ONCE_INIT
+};
+#endif
+
 av_cold void AAC_RENAME(ff_init_ff_sine_windows)(int index) {
     assert(index >= 5 && index < FF_ARRAY_ELEMS(AAC_RENAME(ff_sine_windows)));
 #if !CONFIG_HARDCODED_TABLES
+#ifdef BUILD_TABLES
     AAC_RENAME(ff_sine_window_init)(AAC_RENAME(ff_sine_windows)[index], 1 << index);
+#else
+    ff_thread_once(&init_sine_window_once[index - 5], sine_window_init_func_array[index - 5]);
+#endif
 #endif
 }
 
diff --git a/libavcodec/sinewin_tablegen_template.c b/libavcodec/sinewin_tablegen_template.c
index 43ce1ba82e..7c3c1d8af2 100644
--- a/libavcodec/sinewin_tablegen_template.c
+++ b/libavcodec/sinewin_tablegen_template.c
@@ -23,6 +23,7 @@ 
 #include <stdlib.h>
 #include "libavcodec/aac_defines.h"
 #define CONFIG_HARDCODED_TABLES 0
+#define BUILD_TABLES
 
 #if USE_FIXED
 #define WRITE_FUNC write_int32_t_array