aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2020-11-18 21:38:45 +0100
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2021-02-27 07:20:59 +0100
commitb7d4a08f0d4563f3d411eb4fd692625d03b37e19 (patch)
treef5117c5ff4dd8b274010fb663a31e59b0ce5e75b
parent9475175ec0da425955e0ada4c8d43453215f6c8b (diff)
downloadffmpeg-b7d4a08f0d4563f3d411eb4fd692625d03b37e19.tar.gz
avcodec/fft_template, fft_init_table: Make ff_fft_init() thread-safe
Commit 1af615683e4a1a858407afbaa2fd686842da7e49 put initializing the ff_fft_offsets_lut (which is typically used if FFT_FIXED_32) behind an ff_thread_once() to make ff_fft_init() thread-safe; yet there is a second place where said table may be initialized which is not guarded by this AVOnce: ff_fft_init_mips(). MIPS uses this LUT even for ordinary floating point FFTs, so that ff_fft_init() is not thread-safe (on MIPS) for both 32bit fixed-point as well as floating-point FFTs; e.g. ff_mdct_init() inherits this flaw and therefore initializing e.g. the AAC decoders is not thread-safe (on MIPS) despite them having FF_CODEC_CAP_INIT_CLEANUP set. This commit fixes this by moving the AVOnce to fft_init_table.c and using it to guard all initializations of ff_fft_offsets_lut. (It is not that bad in practice, because every entry of ff_fft_offsets_lut is never read during initialization and is only once ever written to (namely to its final value); but even these are conflicting actions which are (by definition) data races and lead to undefined behaviour.) Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com> (cherry picked from commit b9c1ab89078d862e0146c9d7ed277addd770e3a3)
-rw-r--r--libavcodec/fft_init_table.c24
-rw-r--r--libavcodec/fft_table.h2
-rw-r--r--libavcodec/fft_template.c12
-rw-r--r--libavcodec/mips/fft_mips.c4
4 files changed, 23 insertions, 19 deletions
diff --git a/libavcodec/fft_init_table.c b/libavcodec/fft_init_table.c
index c488018f62..83e35ffb7c 100644
--- a/libavcodec/fft_init_table.c
+++ b/libavcodec/fft_init_table.c
@@ -51,6 +51,8 @@
* @file
* definitions and initialization of LUT table for FFT
*/
+#include "libavutil/thread.h"
+
#include "libavcodec/fft_table.h"
const int32_t ff_w_tab_sr[MAX_FFT_SIZE/(4*16)] = {
@@ -314,15 +316,29 @@ const int32_t ff_w_tab_sr[MAX_FFT_SIZE/(4*16)] = {
uint16_t ff_fft_offsets_lut[21845];
-void ff_fft_lut_init(uint16_t *table, int off, int size, int *index)
+static void fft_lut_init(uint16_t *table, int off, int size, int *index)
{
if (size < 16) {
table[*index] = off >> 2;
(*index)++;
}
else {
- ff_fft_lut_init(table, off, size>>1, index);
- ff_fft_lut_init(table, off+(size>>1), size>>2, index);
- ff_fft_lut_init(table, off+3*(size>>2), size>>2, index);
+ fft_lut_init(table, off, size >> 1, index);
+ fft_lut_init(table, off + (size >> 1), size >> 2, index);
+ fft_lut_init(table, off + 3 * (size >> 2), size >> 2, index);
}
}
+
+static void fft_lut_init_start(void)
+{
+ int n = 0;
+
+ fft_lut_init(ff_fft_offsets_lut, 0, 1 << 17, &n);
+}
+
+void ff_fft_lut_init(void)
+{
+ static AVOnce init_once = AV_ONCE_INIT;
+
+ ff_thread_once(&init_once, fft_lut_init_start);
+}
diff --git a/libavcodec/fft_table.h b/libavcodec/fft_table.h
index ed0a6588b4..09df49f2b8 100644
--- a/libavcodec/fft_table.h
+++ b/libavcodec/fft_table.h
@@ -61,6 +61,6 @@
extern const int32_t ff_w_tab_sr[];
extern uint16_t ff_fft_offsets_lut[];
-void ff_fft_lut_init(uint16_t *table, int off, int size, int *index);
+void ff_fft_lut_init(void);
#endif /* AVCODEC_FFT_TABLE_H */
diff --git a/libavcodec/fft_template.c b/libavcodec/fft_template.c
index 20a62e4290..8825e39f79 100644
--- a/libavcodec/fft_template.c
+++ b/libavcodec/fft_template.c
@@ -35,13 +35,6 @@
#if FFT_FIXED_32
#include "fft_table.h"
-
-static void av_cold fft_lut_init(void)
-{
- int n = 0;
- ff_fft_lut_init(ff_fft_offsets_lut, 0, 1 << 17, &n);
-}
-
#else /* FFT_FIXED_32 */
/* cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse */
@@ -236,10 +229,7 @@ av_cold int ff_fft_init(FFTContext *s, int nbits, int inverse)
#endif
#if FFT_FIXED_32
- {
- static AVOnce control = AV_ONCE_INIT;
- ff_thread_once(&control, fft_lut_init);
- }
+ ff_fft_lut_init();
#else /* FFT_FIXED_32 */
#if FFT_FLOAT
if (ARCH_AARCH64) ff_fft_init_aarch64(s);
diff --git a/libavcodec/mips/fft_mips.c b/libavcodec/mips/fft_mips.c
index 03dcbad4d8..606e7c3e44 100644
--- a/libavcodec/mips/fft_mips.c
+++ b/libavcodec/mips/fft_mips.c
@@ -500,9 +500,7 @@ static void ff_imdct_calc_mips(FFTContext *s, FFTSample *output, const FFTSample
av_cold void ff_fft_init_mips(FFTContext *s)
{
- int n=0;
-
- ff_fft_lut_init(ff_fft_offsets_lut, 0, 1 << 17, &n);
+ ff_fft_lut_init();
ff_init_ff_cos_tabs(17);
#if HAVE_INLINE_ASM