[FFmpeg-cvslog] avcodec/mpegaudiodsp: Make initializing synth windows thread-safe

Andreas Rheinhardt git at videolan.org
Tue Nov 24 12:48:49 EET 2020


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Sun Nov 15 20:56:22 2020 +0100| [16bb8247b45a51c06e27e8563d8a546f3dddc79e] | committer: Andreas Rheinhardt

avcodec/mpegaudiodsp: Make initializing synth windows thread-safe

These arrays are used by the Musepack decoders, the MPEG audio decoders
as well as qdm2 and up until now, these arrays might be initialized more
than once, leading to potential data races as well as unnecessary
initializations. Therefore this commit ensures that each array will only
be initialized once.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=16bb8247b45a51c06e27e8563d8a546f3dddc79e
---

 libavcodec/mpc.c                   |  5 -----
 libavcodec/mpc.h                   |  1 -
 libavcodec/mpc7.c                  |  2 +-
 libavcodec/mpc8.c                  |  3 +--
 libavcodec/mpegaudiodec_template.c |  3 +--
 libavcodec/mpegaudiodsp.h          |  4 ++--
 libavcodec/mpegaudiodsp_template.c | 14 +++++++++++++-
 libavcodec/qdm2.c                  |  3 ++-
 8 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/libavcodec/mpc.c b/libavcodec/mpc.c
index e56b608d8c..e29b823460 100644
--- a/libavcodec/mpc.c
+++ b/libavcodec/mpc.c
@@ -34,11 +34,6 @@
 #include "mpc.h"
 #include "mpcdata.h"
 
-av_cold void ff_mpc_init(void)
-{
-    ff_mpa_synth_init_fixed(ff_mpa_synth_window_fixed);
-}
-
 /**
  * Process decoded Musepack data and produce PCM
  */
diff --git a/libavcodec/mpc.h b/libavcodec/mpc.h
index df462af6b7..704edc9a38 100644
--- a/libavcodec/mpc.h
+++ b/libavcodec/mpc.h
@@ -70,7 +70,6 @@ typedef struct MPCContext {
     DECLARE_ALIGNED(16, int32_t, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
 } MPCContext;
 
-void ff_mpc_init(void);
 void ff_mpc_dequantize_and_synth(MPCContext *c, int maxband, int16_t **out, int channels);
 
 #endif /* AVCODEC_MPC_H */
diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index 6482029efc..e4aa8586d4 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -71,7 +71,6 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
     ff_bswapdsp_init(&c->bdsp);
     ff_mpadsp_init(&c->mpadsp);
     c->bdsp.bswap_buf((uint32_t *) buf, (const uint32_t *) avctx->extradata, 4);
-    ff_mpc_init();
     init_get_bits(&gb, buf, 128);
 
     c->IS = get_bits1(&gb);
@@ -114,6 +113,7 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx)
         }
     }
     vlc_initialized = 1;
+    ff_mpa_synth_init_fixed();
 
     return 0;
 }
diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index 631bac2753..b05942bca7 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -120,8 +120,6 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
     av_lfg_init(&c->rnd, 0xDEADBEEF);
     ff_mpadsp_init(&c->mpadsp);
 
-    ff_mpc_init();
-
     init_get_bits(&gb, avctx->extradata, 16);
 
     skip_bits(&gb, 3);//sample rate
@@ -232,6 +230,7 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
                  &mpc8_q8_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
     }
     vlc_initialized = 1;
+    ff_mpa_synth_init_fixed();
 
     return 0;
 }
diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c
index 12c1964446..849e3e5c09 100644
--- a/libavcodec/mpegaudiodec_template.c
+++ b/libavcodec/mpegaudiodec_template.c
@@ -289,8 +289,6 @@ static av_cold void decode_init_static(void)
                 scale_factor_mult[i][2]);
     }
 
-    RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
-
     /* huffman decode tables */
     offset = 0;
     for (i = 1; i < 16; i++) {
@@ -408,6 +406,7 @@ static av_cold void decode_init_static(void)
         csa_table[i][3] = ca - cs;
 #endif
     }
+    RENAME(ff_mpa_synth_init)();
 }
 
 static av_cold int decode_init(AVCodecContext * avctx)
diff --git a/libavcodec/mpegaudiodsp.h b/libavcodec/mpegaudiodsp.h
index 4c9b05ebac..4577d515d9 100644
--- a/libavcodec/mpegaudiodsp.h
+++ b/libavcodec/mpegaudiodsp.h
@@ -67,8 +67,8 @@ void ff_mpadsp_init_x86_tabs(void);
 void ff_mpadsp_init_mipsfpu(MPADSPContext *s);
 void ff_mpadsp_init_mipsdsp(MPADSPContext *s);
 
-void ff_mpa_synth_init_float(float *window);
-void ff_mpa_synth_init_fixed(int32_t *window);
+void ff_mpa_synth_init_float(void);
+void ff_mpa_synth_init_fixed(void);
 
 void ff_mpadsp_apply_window_float(float *synth_buf, float *window,
                                   int *dither_state, float *samples,
diff --git a/libavcodec/mpegaudiodsp_template.c b/libavcodec/mpegaudiodsp_template.c
index f8d0870df6..c67c456e8a 100644
--- a/libavcodec/mpegaudiodsp_template.c
+++ b/libavcodec/mpegaudiodsp_template.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/mem.h"
+#include "libavutil/thread.h"
 #include "dct32.h"
 #include "mathops.h"
 #include "mpegaudiodsp.h"
@@ -192,7 +193,7 @@ void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
     *synth_buf_offset = offset;
 }
 
-av_cold void RENAME(ff_mpa_synth_init)(MPA_INT *window)
+static av_cold void mpa_synth_init(MPA_INT *window)
 {
     int i, j;
 
@@ -221,6 +222,17 @@ av_cold void RENAME(ff_mpa_synth_init)(MPA_INT *window)
             window[512+128+16*i+j] = window[64*i+48-j];
 }
 
+static av_cold void mpa_synth_window_init(void)
+{
+    mpa_synth_init(RENAME(ff_mpa_synth_window));
+}
+
+av_cold void RENAME(ff_mpa_synth_init)(void)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, mpa_synth_window_init);
+}
+
 /* cos(pi*i/18) */
 #define C1 FIXHR(0.98480775301220805936/2)
 #define C2 FIXHR(0.93969262078590838405/2)
diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c
index 657b2da64d..bd365739ce 100644
--- a/libavcodec/qdm2.c
+++ b/libavcodec/qdm2.c
@@ -1604,11 +1604,12 @@ static av_cold void qdm2_init_static_data(void) {
         return;
 
     qdm2_init_vlc();
-    ff_mpa_synth_init_float(ff_mpa_synth_window_float);
     softclip_table_init();
     rnd_table_init();
     init_noise_samples();
 
+    ff_mpa_synth_init_float();
+
     done = 1;
 }
 



More information about the ffmpeg-cvslog mailing list