From patchwork Wed Dec 2 04:22:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 24318 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 1B9BB447FAC for ; Wed, 2 Dec 2020 06:37:35 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C64D768A773; Wed, 2 Dec 2020 06:24:49 +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 B341368A6F2 for ; Wed, 2 Dec 2020 06:24:41 +0200 (EET) Received: by mail-ej1-f66.google.com with SMTP id ga15so688590ejb.4 for ; Tue, 01 Dec 2020 20:24:41 -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=97GSAX4f/AaXd2GceNyPLSyyMYUA/UTPHTtRPpJ6ASg=; b=Rl4bS2lrhGo6x8/mGR0F/11MjC/cWLmFUg8dM358LthVkNhboJrcElSQrNqK3oNbdh NgZ16QuDx7HNBhoPZXNj/yEtMg8JfOmO6RniB08roJSoFn8738aFA97D2MV1l0CDWBXA RSzSOctXW1GlNnX1X9I9h80/i6LGK1WWVx8ne8DIPqniou6X4IBMgq8KGnO5o1e1crPi mOb+9ppyErRNtoYMcAEKewYcd3kpz10qMaK0N859zJnkPYbR1kmaT0pdUUJCEsNRejjx ifKiVo4cg1yvFQvA/pi2ShLxyxRnd6naHUmPWcXmBCKLtJ71hIZ6J5fYO8rfK/izmPD/ VLag== 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=97GSAX4f/AaXd2GceNyPLSyyMYUA/UTPHTtRPpJ6ASg=; b=OL4y7PEwm2iwyGMajycCA6TJ8Nf4AnjVTl/fscBf0rvaLjDUqAWTJJ685rqIPTFuxT dWxyhlWJVgQO9V+pptFJyArhygrVMg4hFBFoUtjmemXNAAgkxWut56Jlj/8pbplEuDXq 7ZzLv08UXGJ4OtTABY4/cpojqrgN7wRj1P2BKioM6o8RnRze7zTDATBgduspQDmPNAXT rGGt0kXVU3PNAAW+0WW2q3igf6sJiLaUKplBcwqk47C4PJe1kQ6JZYjKBmk4MOOfiVXV RhlsQn/2QD5CUBj8EuDWkQtR2w58eTmQN2YtMWwG4ZLeJvNBVQ7E0qm5ouIgjqjbrbV1 IhaQ== X-Gm-Message-State: AOAM531r+AUJstbdlUpLJYC+UuIrp32A++wNFKenyvnNuKgzwrVPkNMQ espbJhfokYNAnPPuLOnxmzI/yffd3wlR2Q== X-Google-Smtp-Source: ABdhPJyIONglmaFTRsC34nQTnBv5sZu3N5HBGDZ4QpmBtlFsZfID+vrUkU5JYBAIw4XK1R7dNj+fjg== X-Received: by 2002:a17:906:2818:: with SMTP id r24mr589270ejc.100.1606883081019; Tue, 01 Dec 2020 20:24:41 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id mc25sm265087ejb.58.2020.12.01.20.24.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 20:24:40 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 2 Dec 2020 05:22:43 +0100 Message-Id: <20201202042244.519127-82-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201127010249.2724610-1-andreas.rheinhardt@gmail.com> References: <20201127010249.2724610-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 216/217] avcodec/h263data, ituh263*: Make initializing RL tables 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" Up until now, ff_h263_rl_inter and ff_rl_intra_aic were initialized by both ituh263dec and ituh263enc; the result was that although ff_h263_encode_init() guards the initialization of its static data with an AVOnce, initializing the aforementioned RLTables was still not thread-safe because ff_h263_decode_init_vlc() might try to initialize it at the same time. This is fixed by only initializing these RLTables from a single place that is guarded by a dedicated AVOnce. This also makes the Snow encoder actually init-threadsafe; it was already wrongly marked as init-threadsafe since commit d49210788b0836d56dd872d517fe73f83b080101. Signed-off-by: Andreas Rheinhardt --- libavcodec/h263data.c | 16 +++++++++++++++- libavcodec/h263data.h | 2 +- libavcodec/ituh263dec.c | 3 +-- libavcodec/ituh263enc.c | 3 +-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/libavcodec/h263data.c b/libavcodec/h263data.c index f649d58f4e..8fd97100f4 100644 --- a/libavcodec/h263data.c +++ b/libavcodec/h263data.c @@ -25,10 +25,12 @@ #include +#include "libavutil/thread.h" + #include "h263data.h" #include "mpegvideo.h" -uint8_t ff_h263_static_rl_table_store[2][2][2 * MAX_RUN + MAX_LEVEL + 3]; +static uint8_t h263_static_rl_table_store[2][2][2 * MAX_RUN + MAX_LEVEL + 3]; /* intra MCBPC, mb_type = (intra), then (intraq) */ const uint8_t ff_h263_intra_MCBPC_code[9] = { 1, 1, 2, 3, 1, 1, 2, 3, 1 }; @@ -290,3 +292,15 @@ const AVRational ff_h263_pixel_aspect[16] = { { 0, 1 }, { 0, 1 }, }; + +static av_cold void h263_init_rl(void) +{ + ff_rl_init(&ff_h263_rl_inter, h263_static_rl_table_store[0]); + ff_rl_init(&ff_rl_intra_aic, h263_static_rl_table_store[1]); +} + +av_cold void ff_h263_init_rl(void) +{ + static AVOnce init_static_once = AV_ONCE_INIT; + ff_thread_once(&init_static_once, h263_init_rl); +} diff --git a/libavcodec/h263data.h b/libavcodec/h263data.h index 3da0e3771f..4a619f586e 100644 --- a/libavcodec/h263data.h +++ b/libavcodec/h263data.h @@ -61,7 +61,7 @@ extern const int8_t ff_inter_run[102]; extern RLTable ff_h263_rl_inter; extern RLTable ff_rl_intra_aic; -extern uint8_t ff_h263_static_rl_table_store[2][2][2 * MAX_RUN + MAX_LEVEL + 3]; +void ff_h263_init_rl(void); extern const uint16_t ff_h263_format[8][2]; diff --git a/libavcodec/ituh263dec.c b/libavcodec/ituh263dec.c index c1005b0994..7791093cfc 100644 --- a/libavcodec/ituh263dec.c +++ b/libavcodec/ituh263dec.c @@ -122,8 +122,7 @@ av_cold void ff_h263_decode_init_vlc(void) INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33, &ff_mvtab[0][1], 2, 1, &ff_mvtab[0][0], 2, 1, 538); - ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]); - ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]); + ff_h263_init_rl(); INIT_VLC_RL(ff_h263_rl_inter, 554); INIT_VLC_RL(ff_rl_intra_aic, 554); INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15, diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c index 88d003ed47..10d34671e1 100644 --- a/libavcodec/ituh263enc.c +++ b/libavcodec/ituh263enc.c @@ -757,8 +757,7 @@ static av_cold void init_uni_h263_rl_tab(const RLTable *rl, uint8_t *len_tab) static av_cold void h263_encode_init_static(void) { - ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]); - ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]); + ff_h263_init_rl(); init_uni_h263_rl_tab(&ff_rl_intra_aic, uni_h263_intra_aic_rl_len); init_uni_h263_rl_tab(&ff_h263_rl_inter, uni_h263_inter_rl_len);