From patchwork Tue Sep 19 19:57:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 43841 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a886:b0:149:dfde:5c0a with SMTP id ca6csp188356pzb; Tue, 19 Sep 2023 13:02:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEf5aeled2XNoE0qFrLlID1eWbQYlwaOR+WC9W3lHtv1ynA9IJc+vkv+mm6LOfLAwAF4jA9 X-Received: by 2002:a2e:8059:0:b0:2c0:a99:68dd with SMTP id p25-20020a2e8059000000b002c00a9968ddmr405499ljg.4.1695153750170; Tue, 19 Sep 2023 13:02:30 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gg17-20020a170906e29100b009a9f00bdf87si10776176ejb.775.2023.09.19.13.02.29; Tue, 19 Sep 2023 13:02:30 -0700 (PDT) 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; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=OASFrdWF; arc=fail (body hash mismatch); 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 71FFD68C9EF; Tue, 19 Sep 2023 22:58:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01olkn2109.outbound.protection.outlook.com [40.92.65.109]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E004568C949 for ; Tue, 19 Sep 2023 22:58:53 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KZLAVDEb+y9+YNDGS1IIOjhtTJwJq2Q7zZ4BP5OfTpoGEeqnHKBayf33KY/RmPeIRvRf1LxUQ4khD3SK4vYgHG8WNdrxZ4pFCMIGaFsTJakojWXcqfQSgK1KwU8upNqbuNWVwlUOethc3dcnIeSayEmEhRd7kl8/zBZT3s7aq+QPBujbcGfNEQK58jlnxEPSZgXq5RjlOOVIrv73Ppp52KIIuvYYCN1RLNwMwKf4c2w0y4PZfGkSAg8iiWbIvGQ6HbC5pHrANF4o8djAfvbdMfcHThWKCuPX0AwJC+DVaFvl+aaS0kHoDGVZqJCcm1i9JbGwWuNr0aXh5IKgEulEtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=amljfjucEvKWKb+Nr5djzkaNLwaHHz9oKK0P5VgEO4s=; b=fP8j5LFwLIxs7Uiip2D3pk8g17uFSQxI32pUDCxE8qCCPDDKpSv79pay4mYjrwVQKs0kzZ+P0tb7xXy4Opt9926thCiPPuCUQWEvNyTBnao0yPCZ55fSmFOxIyVSgzKbmSVVQO9W9lBr0qjbmw5b2DeVAmENRxbe2cirbS8NFeOrvAVXPDbOWIXL5bXSYDPaIAjXRksZHfLZFm0w56qD3fAE2GMHbAp7j0IyVvZMborM3cpaJUuoKYKEOEHP0cRt0UoNP6cisviCmlbAZxRL+/wOJpakmWbRrwCvazNI8KTdc/BXI8qU1ZKyVCKs8FthOvwyMX20wpvBsM6E5ZFpnQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=amljfjucEvKWKb+Nr5djzkaNLwaHHz9oKK0P5VgEO4s=; b=OASFrdWFV4koZlHDQ5X9H7aFbCzaqDqkCscWxBFss2sYIWJ83zCdt4x+VO0cUWASne6SpsqjUHF8zc8mwQpB3Wq/GubuOoAzW0bzhvG9qyxp563iVx9L9kpVy6N6TqAsNwHobHIS+AvA0pdag7FhqaOZvpSWw0MhN9IDDIbiEro7RvQ41otG54AWe/WQg+1wJpQx0wJ9h9XVat5n/wLICnaIMngPr+T98zWbm9vn/SJMUfVaA+AT/AVLGcqN8VaZtB7k21KUN8APLm/CnjE584rmnqzHIWzWzLGdPtESqv+pXXmb6pkwBfMw2hOjvv75jdk8M62ZEAxzMl4lM/6J2w== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by DU2P250MB0237.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:276::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.26; Tue, 19 Sep 2023 19:58:52 +0000 Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::5e01:aea5:d3a8:cafa]) by AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::5e01:aea5:d3a8:cafa%3]) with mapi id 15.20.6792.026; Tue, 19 Sep 2023 19:58:52 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Sep 2023 21:57:27 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [uAFkaeTailZJmZq3NfqMs3sFlM6qk5d7] X-ClientProxiedBy: ZR0P278CA0086.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:22::19) To AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) X-Microsoft-Original-Message-ID: <20230919195734.1005750-35-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|DU2P250MB0237:EE_ X-MS-Office365-Filtering-Correlation-Id: cd9388dc-61d3-43e6-7306-08dbb94ad927 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lBAMhzeu3PfW+3VM0JSqXI92mXj6TDCKUmJ46XbtGXIc25KXtc2ZUjhQJVsgmoLvm5N5mBhR1IOt03AEz1v+qjkWRwytMK0Q1HXZkW+AM42jENIdp47jnowZ6calJsGM+wj7OZdThVHEHHVgg95Iir44PS5OcIU8tNcOEAz/CByJDCKOnstM2A+dFYY6QAOn3fx0xkmvHqgXUijwUo8XKo3AjZCDO0G/X3eg0+ayMVKFKF2hDZosXCOnrwIenA3cQjNwzZL7DtUQLCQv/ZtPTZ6ZDq1azbDgVNenBv88t1UXTzHCPcZhR10IGMnvFegtKo8S6vBL6IXtHGylQ74JjleG4EyQdHk/1T+A2Sy+By7DCD5uWTVa2KcavH/hr+1TozX4VoTTUJv1flxxpNxnibL8jLuZwkDkzQlf0EpHqo0F9vErK3BtEbzEUDMWM3VYlGLpCuxV4nbetvyFV2vpvLcuyW7aka6IzlfX++dRCX0h49sNQEAaQg6HbZcBW7VEgTWiugLyNu67LShT+EhPL2uzbHfXeFQGawnXJcpmmmvbPMs+GRc3Z9XKKnrkRpFM1QeEk+PSJttoLo201CSMzhp96NFiyYRggB+j+Ytw6ds= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Vc6khqZuUje2EfH9ahOX7yhRFzw4HZV5ulkEqbXBMwx4+/4OBd1TrRI9vLiHGLfYhNyq1Eb4s+aLQUzT47X3di4LCGKnKOJSBn0z5wxKHxQ1mMWITTo1PO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wBSYkY1ZAMrQ50lVg74nzXuU1VH46RiYAZKzTv/Lw/QMuIeZD/jSj+Bqk+h/xrjv2x9+PDL X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cd9388dc-61d3-43e6-7306-08dbb94ad927 X-MS-Exchange-CrossTenant-AuthSource: AS8P250MB0744.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2023 19:58:52.7116 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2P250MB0237 Subject: [FFmpeg-devel] [PATCH 35/42] avcodec/threadprogress: Add new API for frame-threaded progress 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Vvls7X6e73wp The API is very similar by the ProgressFrame API, with the exception that it no longer has an included AVFrame. Instead one can wait on anything via a ThreadProgress. One just has to ensure that the lifetime of the object containing the ThreadProgress is long enough (the corresponding problem for ProgressFrames is solved by allocating the progress and giving each thread a reference to it). This will typically be solved by putting a ThreadProgress in a refcounted structure that is shared between threads. It will be put to the test in the following commits. An alternative to the check for whether the owner exists (meaning "do we use frame-threading?") would be to initialize the progress to INT_MAX in case frame threading is not in use. This would probably be preferable on arches where atomic reads are cheap (like x86), but are there ones where it is not? One could also (guarded by e.g. an ASSERT_LEVEL check) actually track the progress for non-framethreading, too, in order to track errors even in single-threaded mode. Signed-off-by: Andreas Rheinhardt --- libavcodec/pthread_frame.c | 50 +++++++++++++++++++++++++++++++++++++ libavcodec/threadprogress.h | 37 +++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 libavcodec/threadprogress.h diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index 9e827f0606..219ab16ccd 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -40,6 +40,7 @@ #include "refstruct.h" #include "thread.h" #include "threadframe.h" +#include "threadprogress.h" #include "version_major.h" #include "libavutil/avassert.h" @@ -1069,3 +1070,52 @@ void ff_thread_progress_await(const ProgressFrame *f, int n) pthread_cond_wait(&p->progress_cond, &p->progress_mutex); pthread_mutex_unlock(&p->progress_mutex); } + +void ff_thread_progress_init(ThreadProgress *pro, AVCodecContext *owner) +{ + PerThreadContext *p = pro->owner = owner->active_thread_type & FF_THREAD_FRAME ? + owner->internal->thread_ctx : NULL; + if (!p) + return; + atomic_init(&pro->progress, -1); + if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed)) + av_log(owner, AV_LOG_DEBUG, "Initializing ThreadProgress %p\n", (void*)pro); +} + +void ff_thread_progress_report2(ThreadProgress *pro, int n) +{ + PerThreadContext *p = pro->owner; + + if (!p || + atomic_load_explicit(&pro->progress, memory_order_relaxed) >= n) + return; + + if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed)) + av_log(p->avctx, AV_LOG_DEBUG, + "%p finished %d\n", (void*)pro, n); + + pthread_mutex_lock(&p->progress_mutex); + + atomic_store_explicit(&pro->progress, n, memory_order_release); + + pthread_cond_broadcast(&p->progress_cond); + pthread_mutex_unlock(&p->progress_mutex); +} + +void ff_thread_progress_await2(const ThreadProgress *pro, int n) +{ + PerThreadContext *p = pro->owner; + + if (!p || + atomic_load_explicit(&pro->progress, memory_order_acquire) >= n) + return; + + if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed)) + av_log(p->avctx, AV_LOG_DEBUG, + "thread awaiting %d from %p\n", n, (void*)pro); + + pthread_mutex_lock(&p->progress_mutex); + while (atomic_load_explicit(&pro->progress, memory_order_relaxed) < n) + pthread_cond_wait(&p->progress_cond, &p->progress_mutex); + pthread_mutex_unlock(&p->progress_mutex); +} diff --git a/libavcodec/threadprogress.h b/libavcodec/threadprogress.h new file mode 100644 index 0000000000..4f65e7da4d --- /dev/null +++ b/libavcodec/threadprogress.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Andreas Rheinhardt + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_THREADPROGRESS_H +#define AVCODEC_THREADPROGRESS_H + +#include + +struct AVCodecContext; + +typedef struct ThreadProgress { + atomic_int progress; + struct PerThreadContext *owner; +} ThreadProgress; + +void ff_thread_progress_init(ThreadProgress *pro, struct AVCodecContext *owner); +void ff_thread_progress_report2(ThreadProgress *pro, int progress); +void ff_thread_progress_await2(const ThreadProgress *pro, int progress); + +#endif /* AVCODEC_THREADPROGRESS_H */