]> git.sesse.net Git - ffmpeg/commitdiff
avcodec/mpegaudiodsp: Make initializing synth windows thread-safe
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Sun, 15 Nov 2020 19:56:22 +0000 (20:56 +0100)
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Tue, 24 Nov 2020 10:35:03 +0000 (11:35 +0100)
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@gmail.com>
libavcodec/mpc.c
libavcodec/mpc.h
libavcodec/mpc7.c
libavcodec/mpc8.c
libavcodec/mpegaudiodec_template.c
libavcodec/mpegaudiodsp.h
libavcodec/mpegaudiodsp_template.c
libavcodec/qdm2.c

index e56b608d8c035bad6a6a9bb3bdc9dbe917c5b89f..e29b8234602e535cd12723a3525b18dffdc96dfe 100644 (file)
 #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
  */
index df462af6b7503a2bb06354f621f7a262be3554f8..704edc9a384a891d49790c6612090593d797ea81 100644 (file)
@@ -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 */
index 6482029efcf7af370b792ec8bb2eb2329dd393e7..e4aa8586d4794356aa4be55906e7481858c79b6a 100644 (file)
@@ -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;
 }
index 631bac27532d4435c81daeb7aca86a656828c66b..b05942bca71d97b1c7115162073e018886a5b18e 100644 (file)
@@ -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;
 }
index 12c19644461458e8e70b3ba02cbbd94e213e9573..849e3e5c0920d742633d73f64abeee463cc8e72b 100644 (file)
@@ -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)
index 4c9b05ebacbbfdbcc09cdcd9d7517f0b0c95e558..4577d515d9d149f59de2b9fa31057ba182c4efc1 100644 (file)
@@ -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,
index f8d0870df6325a97223c1ace2d8ee99e5642d444..c67c456e8a75d3331f994cd7f5fc1ef14cdc6b8a 100644 (file)
@@ -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)
index 657b2da64dc8fa2721f37b25a70cf43e6f31bd88..bd365739ce3ab6ddce5a363f1871595c92211107 100644 (file)
@@ -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;
 }