From patchwork Fri Nov 20 07:33:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23894 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 1CD5E448F1E for ; Fri, 20 Nov 2020 09:52:30 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 680E268BD86; Fri, 20 Nov 2020 09:34:59 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0995968BD4A for ; Fri, 20 Nov 2020 09:34:48 +0200 (EET) Received: by mail-ej1-f50.google.com with SMTP id o21so11523239ejb.3 for ; Thu, 19 Nov 2020 23:34:48 -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=nHHrCFtvDDlLS5tGV/VsBeALsN8XNsrUngeDMa/N0Yo=; b=PvNXiTxi3tPy64+ijob/pSmQevCAIPyBm99qsaKqNVfey6+/81QzlLLG5vcUX0NGeW s6FcVGcCeDyNPn2fT0+qLUgGDTIIfnizP2hJzomXWnMb2VbQsabi21fMaBQm1845sJ4s DqYRyXn9K6QgY4RScpXfZLhH7mI9Xhr3ez8N84ontN0QMh6mYq797ij+861E15o9pr6K 0RDy5sZtuIoKY1y2p/gmngbo/GgbepJmcsySqumkzbVhUCGAGZ5hT7azmyhxpii/G/E1 ACm39BUC66o9BVdohz7g6gjj5/+s3ZWqV+sK3lyXwLqNHHuW8XzXiE6DtAMrsrqnKXtX 7HcQ== 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=nHHrCFtvDDlLS5tGV/VsBeALsN8XNsrUngeDMa/N0Yo=; b=cceU7ahgWH6O1OUXYG0+KOdqZ8PvWdsfXS5n+TdDOIp5CJ7XYfIItUFIv/X5ezKezW 1NslSVqEZd3kiLvHmzPnV584105kC9CwJQBrv5sLB5g61+yZqdvjjLA3SdyPyDKsKtFo NBSplaMTo8c0a0wXo3E+5bc4BprBZ+CvehmxXfcvlXWpXFTl3Y25QfN2CeWqkNaVPz2b 2EjLTp15VAXjAuJonPGkX2ChPY7fyRoikTB0yGyNB5OFjfLvAvhinC3vk5UEOgZBzVtw E1iJFqN7C61nSos2tT5CkdluqMEIq2lnbgCCZUTcsWNjZBwR8IfG6fPwoJSQ0q4C/p43 znjw== X-Gm-Message-State: AOAM530+TIBagwO67J5tduu5axkhAz3vFduSyEQjQKssDopWBPpIpp8x +oO6uPHb3p5MjqcFZLvH72tAH+3bZq1WbQ== X-Google-Smtp-Source: ABdhPJy385guKlcEJywBRfM1P+Fb8Vl/teV6pMs89skH/HLiqoDuxa5YeaDI7WhLstOHiyGNeLlvIA== X-Received: by 2002:a17:906:d0cd:: with SMTP id bq13mr30600089ejb.372.1605857688233; Thu, 19 Nov 2020 23:34:48 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id i13sm769110ejv.84.2020.11.19.23.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 23:34:47 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Nov 2020 08:33:08 +0100 Message-Id: <20201120073327.820745-44-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 143/162] avcodec/aac*: Make initializing ff_aac_pow*sf_tab 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" This table is currently initialized up to three times: Once by the encoder and twice by the decoders (once by the fixed and once by the floating-point decoder); each of these initializations is guarded by an AVOnce, yet the fact that there are three of them implies that there might be data races (the fact that each entry is only written to once (to its final value) when initializing means that this is safe in practice, yet it is still undefined behaviour). Fix this by only initializing the table from one place that is guarded by one AVOnce. This also avoids unnecessary duplications of the init code. Signed-off-by: Andreas Rheinhardt --- libavcodec/aacenc.c | 12 +---------- libavcodec/aactab.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/aactab.h | 46 +-------------------------------------- 3 files changed, 54 insertions(+), 56 deletions(-) diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c index bb203981b2..274e5ca294 100644 --- a/libavcodec/aacenc.c +++ b/libavcodec/aacenc.c @@ -30,7 +30,6 @@ ***********************************/ #include "libavutil/libm.h" -#include "libavutil/thread.h" #include "libavutil/float_dsp.h" #include "libavutil/opt.h" #include "avcodec.h" @@ -49,8 +48,6 @@ #include "psymodel.h" -static AVOnce aac_table_init = AV_ONCE_INIT; - static void put_pce(PutBitContext *pb, AVCodecContext *avctx) { int i, j; @@ -951,11 +948,6 @@ static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s) return 0; } -static av_cold void aac_encode_init_tables(void) -{ - ff_aac_tableinit(); -} - static av_cold int aac_encode_init(AVCodecContext *avctx) { AACEncContext *s = avctx->priv_data; @@ -1107,10 +1099,8 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) if (HAVE_MIPSDSP) ff_aac_coder_init_mips(s); - if ((ret = ff_thread_once(&aac_table_init, &aac_encode_init_tables)) != 0) - return AVERROR_UNKNOWN; - ff_af_queue_init(avctx, &s->afq); + ff_aac_tableinit(); return 0; } diff --git a/libavcodec/aactab.c b/libavcodec/aactab.c index df551b058f..79dd13d9cb 100644 --- a/libavcodec/aactab.c +++ b/libavcodec/aactab.c @@ -28,7 +28,9 @@ */ #include "libavutil/mem.h" +#include "libavutil/thread.h" #include "aac.h" +#include "aactab.h" #include @@ -3280,3 +3282,53 @@ const DECLARE_ALIGNED(32, int, ff_aac_eld_window_480_fixed)[1800] = { 0xffecff1c, 0xffed391e, 0xffed740c, 0xffedafb1, 0xffedebe1, 0xffee287d, 0xffee654e, 0xffeea23f, }; + +static void aac_tableinit(void) +{ + /* 2^(i/16) for 0 <= i <= 15 */ + static const float exp2_lut[] = { + 1.00000000000000000000, + 1.04427378242741384032, + 1.09050773266525765921, + 1.13878863475669165370, + 1.18920711500272106672, + 1.24185781207348404859, + 1.29683955465100966593, + 1.35425554693689272830, + 1.41421356237309504880, + 1.47682614593949931139, + 1.54221082540794082361, + 1.61049033194925430818, + 1.68179283050742908606, + 1.75625216037329948311, + 1.83400808640934246349, + 1.91520656139714729387, + }; + float t1 = 8.8817841970012523233890533447265625e-16; // 2^(-50) + float t2 = 3.63797880709171295166015625e-12; // 2^(-38) + int t1_inc_cur, t2_inc_cur; + int t1_inc_prev = 0; + int t2_inc_prev = 8; + + for (int i = 0; i < 428; i++) { + t1_inc_cur = 4 * (i % 4); + t2_inc_cur = (8 + 3*i) % 16; + if (t1_inc_cur < t1_inc_prev) + t1 *= 2; + if (t2_inc_cur < t2_inc_prev) + t2 *= 2; + // A much more efficient and accurate way of doing: + // ff_aac_pow2sf_tab[i] = pow(2, (i - POW_SF2_ZERO) / 4.0); + // ff_aac_pow34sf_tab[i] = pow(ff_aac_pow2sf_tab[i], 3.0/4.0); + ff_aac_pow2sf_tab[i] = t1 * exp2_lut[t1_inc_cur]; + ff_aac_pow34sf_tab[i] = t2 * exp2_lut[t2_inc_cur]; + t1_inc_prev = t1_inc_cur; + t2_inc_prev = t2_inc_cur; + } +} + +void ff_aac_tableinit(void) +{ + static AVOnce init_static_once = AV_ONCE_INIT; + ff_thread_once(&init_static_once, aac_tableinit); +} diff --git a/libavcodec/aactab.h b/libavcodec/aactab.h index 29df6a43f0..ce6a7ba1d2 100644 --- a/libavcodec/aactab.h +++ b/libavcodec/aactab.h @@ -42,51 +42,7 @@ extern float ff_aac_pow2sf_tab[428]; extern float ff_aac_pow34sf_tab[428]; -static inline void ff_aac_tableinit(void) -{ - int i; - - /* 2^(i/16) for 0 <= i <= 15 */ - static const float exp2_lut[] = { - 1.00000000000000000000, - 1.04427378242741384032, - 1.09050773266525765921, - 1.13878863475669165370, - 1.18920711500272106672, - 1.24185781207348404859, - 1.29683955465100966593, - 1.35425554693689272830, - 1.41421356237309504880, - 1.47682614593949931139, - 1.54221082540794082361, - 1.61049033194925430818, - 1.68179283050742908606, - 1.75625216037329948311, - 1.83400808640934246349, - 1.91520656139714729387, - }; - float t1 = 8.8817841970012523233890533447265625e-16; // 2^(-50) - float t2 = 3.63797880709171295166015625e-12; // 2^(-38) - int t1_inc_cur, t2_inc_cur; - int t1_inc_prev = 0; - int t2_inc_prev = 8; - - for (i = 0; i < 428; i++) { - t1_inc_cur = 4 * (i % 4); - t2_inc_cur = (8 + 3*i) % 16; - if (t1_inc_cur < t1_inc_prev) - t1 *= 2; - if (t2_inc_cur < t2_inc_prev) - t2 *= 2; - // A much more efficient and accurate way of doing: - // ff_aac_pow2sf_tab[i] = pow(2, (i - POW_SF2_ZERO) / 4.0); - // ff_aac_pow34sf_tab[i] = pow(ff_aac_pow2sf_tab[i], 3.0/4.0); - ff_aac_pow2sf_tab[i] = t1 * exp2_lut[t1_inc_cur]; - ff_aac_pow34sf_tab[i] = t2 * exp2_lut[t2_inc_cur]; - t1_inc_prev = t1_inc_cur; - t2_inc_prev = t2_inc_cur; - } -} +void ff_aac_tableinit(void); /* @name ltp_coef * Table of the LTP coefficients