From patchwork Thu Dec 14 19:31:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 45142 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1225:b0:181:818d:5e7f with SMTP id v37csp5186049pzf; Thu, 14 Dec 2023 11:33:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IEBy5jJVOwlsGvIWpLLvqPB/JITIH6yJnQzHGD5oU7rzAct2TO0utev//TKHAEs44KvmAJl X-Received: by 2002:a17:906:c258:b0:a19:a19b:c701 with SMTP id bl24-20020a170906c25800b00a19a19bc701mr4977933ejb.81.1702582428869; Thu, 14 Dec 2023 11:33:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702582428; cv=none; d=google.com; s=arc-20160816; b=JE2kq2onVZO0aIYUM5YPvzgy8UVoVltn7lk44bmCEEoght7oZ2cbO07Re9sH4vMuVn kYqXxh6JDnyjn3rc19eiuh/JiNZE7uI0nI5Pcck1MwC0KrJVOiYDZw6UFLu0Ekm6gtb5 M6k2K0IT+ec+jWnpxJM6r1Jn4+znnnTOl2xWn9sPOtAqqa7Aaoby/FOozaoQM7olneGX UlUe/0SlCzh8owPgb6tV6pwujgOVwq/23yp0KtwR97f2y6sfBxsZ8XHgoNu7tZK2RPqy qd/jaTil7oEFu8cScB62G5hT4NDNmhqkS6LNnbY7u8lorXdJgclk3qVNwEIpNuka3c1U hfzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=lOiQ9UanJWPphkDa15C9CAjPxB0hkhu8QimNIdFDrJI=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=BOuhYr3pOY2NOEGlzhzsBbkBYvJVRV6vmXAebcQMPa6FJKvxqS6KdrAv37OmTe0sdf HX0HDnep8jv/mIm+O9ZMOsR3+gJlfIFTSJSEzrEYL+FOa7ybDj0smoFXAyWHba/3laem 6E1H0diardJJv5xMVhWxTtMDD1kYIx06uz0CVBTmmL5zUq44t0TLtVlXrlCKXdGNc1ge KkwDcx8eIcuylhJuSSFXQkw+0k3vOzQyv0Sj9zQwksVaVyP6Juw5cmQ66iXxFP4vI7Z/ 1vgXZVigJbr6UwCPLinBE880FYXgZttmm0sIvOlMMRmiMNvcGhPX/8zUc9S8TnScZTrF fc/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s11-20020a170906bc4b00b00a1dda250ee2si6876846ejv.0.2023.12.14.11.33.43; Thu, 14 Dec 2023 11:33:48 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8A87E68D2B0; Thu, 14 Dec 2023 21:32:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FACD68D28B for ; Thu, 14 Dec 2023 21:32:45 +0200 (EET) Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id AF8C71BA4 for ; Thu, 14 Dec 2023 20:32:44 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id YtTayiXFWUNG for ; Thu, 14 Dec 2023 20:32:44 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 947221CAF for ; Thu, 14 Dec 2023 20:32:38 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id A58BD3A073A for ; Thu, 14 Dec 2023 20:32:31 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 14 Dec 2023 20:31:38 +0100 Message-ID: <20231214193138.2503-9-anton@khirnov.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231214193138.2503-1-anton@khirnov.net> References: <20231214193138.2503-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: use a mutex for enc_stats_write() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: nAccYDy4NeRB It may be called concurrently from different threads to write into the same file. --- fftools/ffmpeg.h | 3 +++ fftools/ffmpeg_enc.c | 4 ++++ fftools/ffmpeg_mux.c | 4 ++++ fftools/ffmpeg_mux_init.c | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 03dbb528c0..33a29b316f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -480,6 +480,9 @@ typedef struct EncStats { int nb_components; AVIOContext *io; + + pthread_mutex_t lock; + int lock_initialized; } EncStats; extern const char *const forced_keyframes_const_names[]; diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index d774a7e008..57590a43a3 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -491,6 +491,8 @@ void enc_stats_write(OutputStream *ost, EncStats *es, ptsi = fd->dec.pts; } + pthread_mutex_lock(&es->lock); + for (size_t i = 0; i < es->nb_components; i++) { const EncStatsComponent *c = &es->components[i]; @@ -538,6 +540,8 @@ void enc_stats_write(OutputStream *ost, EncStats *es, } avio_w8(io, '\n'); avio_flush(io); + + pthread_mutex_unlock(&es->lock); } static inline double psnr(double d) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 357f34172f..ab86abee14 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -778,6 +778,10 @@ static void enc_stats_uninit(EncStats *es) for (int i = 0; i < es->nb_components; i++) av_freep(&es->components[i].str); av_freep(&es->components); + + if (es->lock_initialized) + pthread_mutex_destroy(&es->lock); + es->lock_initialized = 0; } static void ost_free(OutputStream **post) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 0203701d78..52eca9f9d5 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -402,6 +402,11 @@ fail: return ret; } + ret = pthread_mutex_init(&es->lock, NULL); + if (ret) + return AVERROR(ret); + es->lock_initialized = 1; + ret = enc_stats_get_file(&es->io, path); if (ret < 0) return ret;