diff mbox series

[FFmpeg-devel,169/191] avcodec/atrac3plus: Make decoder init-threadsafe

Message ID 20201123193739.1249300-7-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
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/atrac3plus.h    |  4 ++--
 libavcodec/atrac3plusdec.c | 16 +++++++++++-----
 libavcodec/atrac3plusdsp.c |  8 ++++----
 3 files changed, 17 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/atrac3plus.h b/libavcodec/atrac3plus.h
index 3c39e293c7..82fe0fe1f8 100644
--- a/libavcodec/atrac3plus.h
+++ b/libavcodec/atrac3plus.h
@@ -179,9 +179,9 @@  int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
 void ff_atrac3p_init_imdct(AVCodecContext *avctx, FFTContext *mdct_ctx);
 
 /**
- * Initialize sine waves synthesizer.
+ * Initialize sine waves synthesizer and ff_sine_* tables.
  */
-void ff_atrac3p_init_wave_synth(void);
+void ff_atrac3p_init_dsp_static(void);
 
 /**
  * Synthesize sine waves for a particular subband.
diff --git a/libavcodec/atrac3plusdec.c b/libavcodec/atrac3plusdec.c
index 4b008ba0ee..b75f7eab5e 100644
--- a/libavcodec/atrac3plusdec.c
+++ b/libavcodec/atrac3plusdec.c
@@ -39,6 +39,7 @@ 
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/thread.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "internal.h"
@@ -144,8 +145,15 @@  static av_cold int set_channel_params(ATRAC3PContext *ctx,
     return 0;
 }
 
+static av_cold void atrac3p_init_static(void)
+{
+    ff_atrac3p_init_vlcs();
+    ff_atrac3p_init_dsp_static();
+}
+
 static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
 {
+    static AVOnce init_static_once = AV_ONCE_INIT;
     ATRAC3PContext *ctx = avctx->priv_data;
     int i, ch, ret;
 
@@ -154,8 +162,6 @@  static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    ff_atrac3p_init_vlcs();
-
     /* initialize IPQF */
     ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
 
@@ -163,8 +169,6 @@  static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
 
     ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
 
-    ff_atrac3p_init_wave_synth();
-
     if ((ret = set_channel_params(ctx, avctx)) < 0)
         return ret;
 
@@ -194,6 +198,8 @@  static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
 
     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
 
+    ff_thread_once(&init_static_once, atrac3p_init_static);
+
     return 0;
 }
 
@@ -405,7 +411,7 @@  AVCodec ff_atrac3pal_decoder = {
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = AV_CODEC_ID_ATRAC3PAL,
     .capabilities   = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
     .priv_data_size = sizeof(ATRAC3PContext),
     .init           = atrac3p_decode_init,
     .close          = atrac3p_decode_close,
diff --git a/libavcodec/atrac3plusdsp.c b/libavcodec/atrac3plusdsp.c
index ca3154e133..7af1fa2141 100644
--- a/libavcodec/atrac3plusdsp.c
+++ b/libavcodec/atrac3plusdsp.c
@@ -79,9 +79,6 @@  const float ff_atrac3p_mant_tab[8] = {
 
 av_cold void ff_atrac3p_init_imdct(AVCodecContext *avctx, FFTContext *mdct_ctx)
 {
-    ff_init_ff_sine_windows(7);
-    ff_init_ff_sine_windows(6);
-
     /* Initialize the MDCT transform. */
     ff_mdct_init(mdct_ctx, 8, 1, -1.0);
 }
@@ -94,7 +91,7 @@  static DECLARE_ALIGNED(32, float, sine_table)[2048]; ///< wave table
 static DECLARE_ALIGNED(32, float, hann_window)[256]; ///< Hann windowing function
 static float amp_sf_tab[64];   ///< scalefactors for quantized amplitudes
 
-av_cold void ff_atrac3p_init_wave_synth(void)
+av_cold void ff_atrac3p_init_dsp_static(void)
 {
     int i;
 
@@ -109,6 +106,9 @@  av_cold void ff_atrac3p_init_wave_synth(void)
     /* generate amplitude scalefactors table */
     for (i = 0; i < 64; i++)
         amp_sf_tab[i] = exp2f((i - 3) / 4.0f);
+
+    ff_init_ff_sine_windows(7);
+    ff_init_ff_sine_windows(6);
 }
 
 /**