From patchwork Wed Dec 2 04:22:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 24252 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 24DE944A4E3 for ; Wed, 2 Dec 2020 06:24:46 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0201768A73D; Wed, 2 Dec 2020 06:24:46 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f65.google.com (mail-ej1-f65.google.com [209.85.218.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1D3DC68A688 for ; Wed, 2 Dec 2020 06:24:38 +0200 (EET) Received: by mail-ej1-f65.google.com with SMTP id x16so653808ejj.7 for ; Tue, 01 Dec 2020 20:24:38 -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=TsmleXjaZAUp0ZbRzWt+XpgRU5+1OR/W88+g7V3+BMg=; b=NIkbs7DI6eFx2BaswAWcGqnSmW3xkk2/SFuWTZ1GZy6l6Xq7vbdkfWvM7+VGrJkXzB NBv4+p9S6mUPz3lRXnSj4Af01+ym68z4/RlUm3M43L/pBBgYUHD16oWKI8nFMkt/9vhO 2qnwUw2xdMcAwIkfU6cTlnrxSAUSwZZyaf34fMIUpq2FEChaaNz21i2S6PftSaUcwuAG PgK0Oo61x9tqC9N87Ti3ed9jktHWElEukDOp542Iq6ACBvN8/iRODjf3s8HR1Vp2CEJZ OV99Jv3PxO1+tMeKLCWB3CFhzfOVFKalxga+E8r6IYYxS7RkvM9Mvn7b54vMEab/d4S+ 4Arg== 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=TsmleXjaZAUp0ZbRzWt+XpgRU5+1OR/W88+g7V3+BMg=; b=YRO87kIImOliiPrB7USSFNp2xh+ZR/LPi0CWu/dmJ7jY32mVehVrGYkT7T4eoLulBC EaSZbyOvUObCGl2QHsNLsLaWDXN53ZyWoxIJhWJ7ZYln5A6BaQNixrsRk+rrekipQX3j Ko8pAKhArHJemtcn8Gl80Qwh0e/SNCdJPXYFgUWnfUlkHPISaFDnT9tiRRVKeQifIEzu iqjYk4EpiHbFoFyqttwX7PsXvo8N0LAeLJlDN37Pry41hAaJRR3vepYCye9mBUZDC4Ue kHnytI9/7oF4PSno0s6SX/MPKGiI9YEwprTox+2I63Cd5cSjtB3l9fKS7dPPL4Qz76MX B4UQ== X-Gm-Message-State: AOAM532UfCf/Ji1ERcE4vut/syRyj42uqeDwP0fJKZBgzt9RLIOrKWuc sbo8WfWQkm09BUg2N0bVWzCBi09azip+9w== X-Google-Smtp-Source: ABdhPJxqX9/utFTybESIpVF1J8NLVCB55/Hd2KKolwOQjp4fdjwvjCCTR8W8rafpRD+qCFmp3RYj7Q== X-Received: by 2002:a17:906:ae55:: with SMTP id lf21mr587404ejb.101.1606883077350; Tue, 01 Dec 2020 20:24:37 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 20:24:36 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 2 Dec 2020 05:22:39 +0100 Message-Id: <20201202042244.519127-78-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 212/217] avcodec/snow: Fix race in ff_snow_common_init() 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" Commits d49210788b0836d56dd872d517fe73f83b080101 and ee8ce211ead04c8684da0c9c143814e57e9b9eda set the FF_CODEC_CAP_INIT_THREADSAFE flag for the Snow encoder resp. decoder; yet these codecs init functions aren't threadsafe at all: ff_snow_common_init() initializes static data, but there is no check at all that it is only done once by one thread. This commit adds such checks; this makes the decoder init-threadsafe as long as the stack is properly aligned. Signed-off-by: Andreas Rheinhardt --- libavcodec/snow.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/libavcodec/snow.c b/libavcodec/snow.c index 066efc5171..bb65a6f43f 100644 --- a/libavcodec/snow.c +++ b/libavcodec/snow.c @@ -21,6 +21,7 @@ #include "libavutil/intmath.h" #include "libavutil/log.h" #include "libavutil/opt.h" +#include "libavutil/thread.h" #include "avcodec.h" #include "me_cmp.h" #include "snow_dwt.h" @@ -427,10 +428,19 @@ mca( 8, 0,8) mca( 0, 8,8) mca( 8, 8,8) +static av_cold void snow_static_init(void) +{ + for (int i = 0; i < MAX_REF_FRAMES; i++) + for (int j = 0; j < MAX_REF_FRAMES; j++) + ff_scale_mv_ref[i][j] = 256 * (i + 1) / (j + 1); + init_qexp(); +} + av_cold int ff_snow_common_init(AVCodecContext *avctx){ + static AVOnce init_static_once = AV_ONCE_INIT; SnowContext *s = avctx->priv_data; int width, height; - int i, j; + int i; s->avctx= avctx; s->max_ref_frames=1; //just make sure it's not an invalid value in case of no initial keyframe @@ -480,8 +490,6 @@ av_cold int ff_snow_common_init(AVCodecContext *avctx){ mcfh(0, 8) mcfh(8, 8) - init_qexp(); - // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift); width= s->avctx->width; @@ -495,8 +503,6 @@ av_cold int ff_snow_common_init(AVCodecContext *avctx){ return AVERROR(ENOMEM); for(i=0; ilast_picture[i] = av_frame_alloc(); if (!s->last_picture[i]) return AVERROR(ENOMEM); @@ -507,6 +513,8 @@ av_cold int ff_snow_common_init(AVCodecContext *avctx){ if (!s->mconly_picture || !s->current_picture) return AVERROR(ENOMEM); + ff_thread_once(&init_static_once, snow_static_init); + return 0; }