From patchwork Fri Nov 20 07:19:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23814 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 B3E1744AD73 for ; Fri, 20 Nov 2020 09:35:09 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DAACB68BB85; Fri, 20 Nov 2020 09:25:22 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f68.google.com (mail-ej1-f68.google.com [209.85.218.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A434B68BB23 for ; Fri, 20 Nov 2020 09:25:10 +0200 (EET) Received: by mail-ej1-f68.google.com with SMTP id y17so11454368ejh.11 for ; Thu, 19 Nov 2020 23:25:10 -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=NdDhb+zJskZMYqIOWFKb0mbE4OncFdgKp7guwyMWm68=; b=KnZll9AdMy2YH/2eRNUf6+ECIE1a5rYvJ7MThu4LaOSaPk0sa2O3spSwtFJ81lCloK tIkrecXveuo1X9mVaSIl2lzYrcseMKnZv6vpY2lZ3NepTr1YyvC/lKQdiQWSjH6i4UvO 8jyNWZVrgUJmeV8eOKJAbatS2IlHy8FijkOtM0Ppizvt/2DfjG/Q+1G1FH1520kvHg6Y dhmMbUidY87Ifum3Nqt6pMtPlvlVtMg9AZ7VsbkJDgYDwBp9Ei/MC0IzkGrmi5Z2gcKU 2wo2/ImmnPt/hLp2ciTsshhePzpezYJTSe1ZCv9ThOp0XrbgRTCn21xp6fTYRSvhQw5h hwjA== 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=NdDhb+zJskZMYqIOWFKb0mbE4OncFdgKp7guwyMWm68=; b=fvM1vd5chk13DpOnPpLfl3NfLZUPi7YmnDSrW86vO2Ymm6IPr1VLRlBccDZOfUNHTJ F6wN/RFmPnyKMsGDuBW5x1lqZ7XfcnWeVH9iTvRpF0xoKvFrIb9t0MiO3vf9G6M0ax8W poFdVOMFGvrdL3laEHO2M1jNRBOE6GS2Y/m+Idmp/1vTABLhHkRtrxLF/qP57gPAO/wD gbT8Ob+9wcgL3D1l0IYc/lycswHxcLITYrpqKwSn+U5/ePAz8oBX67TVOvaFNK41CepX BRyyksPjN1foKJs5jaE8kFIHG8eUgjfZ/PQDH+yk3WunQyehKElmnJNyxKLM4XgbyG3J A6GQ== X-Gm-Message-State: AOAM531oHVBJE+YGbPy5LxdKMLWqVGEpYvWMBTCrq6F3/nCfNNYwfJ/N BHF6pb3a1vLr35ncHS2xTRiGEqXjwqY/Yw== X-Google-Smtp-Source: ABdhPJxw7UiEiW9P7thG5Eeah5XIlVlI+DTvS7+bcGuHv1qQsvtYaYJnrT3Es3fZ8BfYp8R/ACdVeQ== X-Received: by 2002:a17:907:20ac:: with SMTP id pw12mr4219688ejb.245.1605857109863; Thu, 19 Nov 2020 23:25:09 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 23:25:09 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Nov 2020 08:19:32 +0100 Message-Id: <20201120072116.818090-60-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 059/162] avcodec/mpc7: Make decoder init-threadsafe 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" Signed-off-by: Andreas Rheinhardt --- libavcodec/mpc7.c | 65 +++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c index b067d77c4b..720d67623f 100644 --- a/libavcodec/mpc7.c +++ b/libavcodec/mpc7.c @@ -28,6 +28,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/internal.h" #include "libavutil/lfg.h" +#include "libavutil/thread.h" #include "avcodec.h" #include "get_bits.h" #include "internal.h" @@ -38,16 +39,42 @@ static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2]; +static av_cold void mpc7_init_static(void) +{ + static VLC_TYPE quant_tables[7224][2]; + const uint8_t *raw_quant_table = mpc7_quant_vlcs; + + INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE, + &mpc7_scfi[1], 2, + &mpc7_scfi[0], 2, 1, 0, 0, 1 << MPC7_SCFI_BITS); + INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE, + &mpc7_dscf[1], 2, + &mpc7_dscf[0], 2, 1, -7, 0, 1 << MPC7_DSCF_BITS); + INIT_VLC_STATIC_FROM_LENGTHS(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE, + &mpc7_hdr[1], 2, + &mpc7_hdr[0], 2, 1, -5, 0, 1 << MPC7_HDR_BITS); + for (unsigned i = 0, offset = 0; i < MPC7_QUANT_VLC_TABLES; i++){ + for (int j = 0; j < 2; j++) { + quant_vlc[i][j].table = &quant_tables[offset]; + quant_vlc[i][j].table_allocated = FF_ARRAY_ELEMS(quant_tables) - offset; + ff_init_vlc_from_lengths(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i], + &raw_quant_table[1], 2, + &raw_quant_table[0], 2, 1, + mpc7_quant_vlc_off[i], + INIT_VLC_STATIC_OVERLONG, NULL); + raw_quant_table += 2 * mpc7_quant_vlc_sizes[i]; + offset += quant_vlc[i][j].table_size; + } + } + ff_mpa_synth_init_fixed(); +} + static av_cold int mpc7_decode_init(AVCodecContext * avctx) { - int j; + static AVOnce init_static_once = AV_ONCE_INIT; MPCContext *c = avctx->priv_data; GetBitContext gb; LOCAL_ALIGNED_16(uint8_t, buf, [16]); - static int vlc_initialized = 0; - - static VLC_TYPE quant_tables[7224][2]; - const uint8_t *raw_quant_table = mpc7_quant_vlcs; /* Musepack SV7 is always stereo */ if (avctx->channels != 2) { @@ -83,32 +110,7 @@ static av_cold int mpc7_decode_init(AVCodecContext * avctx) avctx->sample_fmt = AV_SAMPLE_FMT_S16P; avctx->channel_layout = AV_CH_LAYOUT_STEREO; - if(vlc_initialized) return 0; - av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n"); - INIT_VLC_STATIC_FROM_LENGTHS(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE, - &mpc7_scfi[1], 2, - &mpc7_scfi[0], 2, 1, 0, 0, 1 << MPC7_SCFI_BITS); - INIT_VLC_STATIC_FROM_LENGTHS(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE, - &mpc7_dscf[1], 2, - &mpc7_dscf[0], 2, 1, -7, 0, 1 << MPC7_DSCF_BITS); - INIT_VLC_STATIC_FROM_LENGTHS(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE, - &mpc7_hdr[1], 2, - &mpc7_hdr[0], 2, 1, -5, 0, 1 << MPC7_HDR_BITS); - for (unsigned i = 0, offset = 0; i < MPC7_QUANT_VLC_TABLES; i++){ - for(j = 0; j < 2; j++){ - quant_vlc[i][j].table = &quant_tables[offset]; - quant_vlc[i][j].table_allocated = FF_ARRAY_ELEMS(quant_tables) - offset; - ff_init_vlc_from_lengths(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i], - &raw_quant_table[1], 2, - &raw_quant_table[0], 2, 1, - mpc7_quant_vlc_off[i], - INIT_VLC_STATIC_OVERLONG, NULL); - raw_quant_table += 2 * mpc7_quant_vlc_sizes[i]; - offset += quant_vlc[i][j].table_size; - } - } - vlc_initialized = 1; - ff_mpa_synth_init_fixed(); + ff_thread_once(&init_static_once, mpc7_init_static); return 0; } @@ -317,4 +319,5 @@ AVCodec ff_mpc7_decoder = { .capabilities = AV_CODEC_CAP_DR1, .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE }, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, };