From patchwork Fri Nov 20 07:19:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23813 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6FDA344AD73 for ; Fri, 20 Nov 2020 09:34:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EB59F68BB80; Fri, 20 Nov 2020 09:25:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D173068BB0F for ; Fri, 20 Nov 2020 09:25:09 +0200 (EET) Received: by mail-ej1-f66.google.com with SMTP id a16so11513591ejj.5 for ; Thu, 19 Nov 2020 23:25:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=DJoeplEUVrcPmSibUHdttKLWcBqJ2pklRDiZ+KN7pss=; b=ZD5tcqX2bM+K5Q+0PFgrTgFtpW82hTikAy9Oxf2QmzJugelEv+aBA+gaXpLrUQpKFN S6y77l16jry/aKKASli3PC32Wb07FSSOmMr4sLMGu9riplUjcITGcVV3n+n2KK914cbx Z/bmniSyiKnMXazMYu2k6JIXK7dTunAdLUKjRV/AWHBd1Mf5hDkvc0g8KHxf1e6duFVm CuTJLlsupkloj8vviSSq53auGQg/636TQdUS7dM/61h8nDnJOzP0v4YFeZm6TU/oYdQ1 9jFLSZtUTK1K2l93f8uhuVUDlrxYHLiQ2ZeO4xkbdkVyCrFkXiByZGF0UXDBGbJbof7x lv0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=DJoeplEUVrcPmSibUHdttKLWcBqJ2pklRDiZ+KN7pss=; b=coxHq9zRAEduGQKFXZ8vhAXhfDjZV9si+1sprHCTZpQa8adwNmHCKLdF9JPC0WHAU+ dR4J8neQZZDPHdVNz3v8c/w/Noa1uSAGwRe2kHcAF5/pmHWycLLIs+766iClQOn13rkm TXdZ4XPjv3RnVdXDMlxOkTrz+022hWyl7w7h4JWqgNp4oVWjZNHsLrRosONhbykGP9ux 0kb0H2iUSqKbrs2KiXAvvS2lr3ghK1DtTeVYuSIrB9Fn6ih1hqy86FX+0NwAD7NABO6S JEdY1UddCWYUvnxrD5qnCerOAEMVS5qI1YSt85myWCoIrVuwoMAeU32G1cHMhDIH8ilG 9sgw== X-Gm-Message-State: AOAM530kxN0sCXOF4GV0d08aVnHyGoQ31pq2Uvyztdu2N2YNJYeZ9KGX oBrROpmVYsbSTbJuELnPPn4s7tGDrMBt9A== X-Google-Smtp-Source: ABdhPJwEWQ2H1ms0JOZivWcYMHWruLEWUMqLgqPmgFjM0lj1l773VTToq/LfQyW+/0yWB0ZxY0ozsA== X-Received: by 2002:a17:906:8496:: with SMTP id m22mr29785204ejx.298.1605857108890; Thu, 19 Nov 2020 23:25:08 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id lz27sm779419ejb.39.2020.11.19.23.25.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 23:25:08 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Nov 2020 08:19:31 +0100 Message-Id: <20201120072116.818090-59-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201120072116.818090-1-andreas.rheinhardt@gmail.com> References: <20201120072116.818090-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 058/162] avcodec/mpegaudiodsp: Make initializing synth windows thread-safe X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 --- 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 0b9e6f0088..b067d77c4b 100644 --- a/libavcodec/mpc7.c +++ b/libavcodec/mpc7.c @@ -64,7 +64,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); @@ -109,6 +108,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 b36e47ce66..c263762c8b 100644 --- a/libavcodec/mpc8.c +++ b/libavcodec/mpc8.c @@ -126,8 +126,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 @@ -171,6 +169,7 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx) &q_syms, -((8 << j) - 1)); } 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; }