diff mbox series

[FFmpeg-devel,187/191] avcodec/dsd: Make initializing DSD tables thread-safe

Message ID 20201123193739.1249300-25-andreas.rheinhardt@gmail.com
State New
Headers show
Series VLC, esp. init_vlc patches
Related show

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
This automatically makes the DSD formats as well as DST and WavPack
init-threadsafe.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/dsd.c     | 8 +++-----
 libavcodec/dsddec.c  | 1 +
 libavcodec/dstdec.c  | 1 +
 libavcodec/wavpack.c | 3 ++-
 4 files changed, 7 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/dsd.c b/libavcodec/dsd.c
index d48f87fa0f..95aab61ea4 100644
--- a/libavcodec/dsd.c
+++ b/libavcodec/dsd.c
@@ -21,6 +21,7 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/thread.h"
 #include "libavcodec/internal.h"
 #include "libavcodec/mathops.h"
 #include "avcodec.h"
@@ -45,11 +46,8 @@  static av_cold void dsd_ctables_tableinit(void)
 
 av_cold void ff_init_dsd_data(void)
 {
-    static int done = 0;
-    if (done)
-        return;
-    dsd_ctables_tableinit();
-    done = 1;
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, dsd_ctables_tableinit);
 }
 
 void ff_dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
diff --git a/libavcodec/dsddec.c b/libavcodec/dsddec.c
index 39837a5ad9..375e49341f 100644
--- a/libavcodec/dsddec.c
+++ b/libavcodec/dsddec.c
@@ -125,6 +125,7 @@  AVCodec ff_##name_##_decoder = { \
     .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS, \
     .sample_fmts  = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \
                                                    AV_SAMPLE_FMT_NONE }, \
+    .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
 };
 
 DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first")
diff --git a/libavcodec/dstdec.c b/libavcodec/dstdec.c
index 41e761d7e5..3fd710d2f6 100644
--- a/libavcodec/dstdec.c
+++ b/libavcodec/dstdec.c
@@ -389,4 +389,5 @@  AVCodec ff_dst_decoder = {
     .capabilities   = AV_CODEC_CAP_DR1,
     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
                                                       AV_SAMPLE_FMT_NONE },
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
 };
diff --git a/libavcodec/wavpack.c b/libavcodec/wavpack.c
index e02a56aa35..8fdabce335 100644
--- a/libavcodec/wavpack.c
+++ b/libavcodec/wavpack.c
@@ -1712,5 +1712,6 @@  AVCodec ff_wavpack_decoder = {
     .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
                       AV_CODEC_CAP_SLICE_THREADS,
-    .caps_internal  = FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP |
+                      FF_CODEC_CAP_INIT_THREADSAFE,
 };