From patchwork Tue Apr 9 06:33:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47965 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:9c8d:b0:1a7:a0dc:8de5 with SMTP id mj13csp170517pzb; Mon, 8 Apr 2024 23:34:49 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVuGqKvm7m/ZVN/5IGfCvXC2iOKvqtS7sBXg1ahZ9qCn1fMKLJJJ/4vvNvjdX7THScbKfkBrcjvr/ZDMNKyTSKVB+GIHNq3tZIiUg== X-Google-Smtp-Source: AGHT+IHUfjDze78CNJ0jOgXvKin/eft2Vz6ma2UQ6UfHqaW7b3tajgi7Xkfb9wP/gf0u4MVBiVyB X-Received: by 2002:a17:907:11c8:b0:a51:9cc5:1605 with SMTP id va8-20020a17090711c800b00a519cc51605mr7887970ejb.4.1712644488858; Mon, 08 Apr 2024 23:34:48 -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 dt6-20020a170907728600b00a4739d9d28fsi4411258ejc.556.2024.04.08.23.34.47; Mon, 08 Apr 2024 23:34:48 -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=s+rQEjGm; 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 28B1968D3B8; Tue, 9 Apr 2024 09:34:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03olkn2076.outbound.protection.outlook.com [40.92.59.76]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FFC768D29B for ; Tue, 9 Apr 2024 09:34:36 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kWA33FGazaAbPKoQsVE28MQh3nzRifAOIm91DM3fLM12qlc85fw1EY/ZOReGvEZy3ppAjS8FDv3BkzE4W8sJze+dyUpGiXVcgiqa+lwrECOHyrYxdnlfvnvyW0+ZyKhZCVts3WFinRblqSmnyKRNZMM9ivY/ahea8Pika2Y/H2zKnZC7fVpqit8zf+3j2uCi4z1SpIgQPnbEiErLVdIqQST+oPiPzbcLSO4+lyJr/GFlpP/B23a36t0cbEIViHYTW0nJoqAisnBQujIE/RiCcDox7joz7HHDDJBCj0ZAhvMXi+dc3cgf52LdIkMcRRtssCBMF95N/aVKmt/NvBoC/w== 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=Q/Wr13JGIXMs3p9ZAkFYBkAfWMH+wGM8YdRl69NDEVc=; b=kPx/BWnVibB5H96LGLuLQ3uhp7LS/Z+Rn9cnt+N+xv6npPd3Deujg7dte/wD8dGIdYhuOWwrRXehtv5r82yXA/ITwrqTeB7P5lFKhAsONYyntSHKGHcfGJxuS85HiVRjWqW7UtCLn+hEpTxkzNQvOzzZnxhWesh2CuxP3697JZodYm5VfPwgItXRSGlga3fVRVbXP1eaduV/ZSYRl3Ec54jS2shr4d3XOLBYT1ipHI/sffsUe6HEEVtdLhj0K6wNRMoBpPBi4ppqrJgpqINGGQicdf8SVnPEJSIUPmPjeE5rKQ31V/dn6UQdGvEbWoGBZLqLL9OHV6LsXvuz45Ex2Q== 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=Q/Wr13JGIXMs3p9ZAkFYBkAfWMH+wGM8YdRl69NDEVc=; b=s+rQEjGmnIhAREM00b71V7NPFi8BRNwCVZMg09D1i8EUKzPEbQ+bwiBC1jPdtIItwZ3edRmCJk608auvGyK+MN2qB/BbhebCibFvs/OvlXcUUOCKKBQ7g5R29Nw/CSXKcMHoYhnXEfzUHPfoVGks4nhS31YnKy8HNOkchnYTcCy1leSP+ZVrJFHheUT0DIJZUuU898LdZvqQEzdEOPUqdvEI2YnbQ2+9Z7Uag9vb4F0v5TQV7RDoIu0IyVh7LJ01rHHkCxwrjQ57iTd6YUFRuKooW7oCXAYnJPL4YlWCneF6I1CaZSVf8pD2EVbihnf1cXCwXRoYhsuCZLsPCwTWAg== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by PR3P250MB0260.EURP250.PROD.OUTLOOK.COM (2603:10a6:102:17f::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Tue, 9 Apr 2024 06:34:33 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d%4]) with mapi id 15.20.7409.042; Tue, 9 Apr 2024 06:34:33 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 9 Apr 2024 08:33:54 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [fTDgmk5iycrlnjg0q0sWzh7rddtFpM8IEEH9qNafYrw=] X-ClientProxiedBy: FR0P281CA0227.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:b2::7) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240409063420.2183771-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|PR3P250MB0260:EE_ X-MS-Office365-Filtering-Correlation-Id: 90c6fe17-09f8-40f2-c467-08dc585f1e5a X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1ciZcm5NakCv1y1BiHLkRjlorj62t+O6NM+ZG8cGSUt4Vw4u/Da7ZIPxD2wLTu9+tjixgzD7joAwVdAFQ3i1KKmikvsQbMa2we0hMLRCunphkV5CDDsTOsM5sEjrKZEL7Owy0pSlcIkZPVlSOPIA+RKelCfq9WebGBCwpE06mNln8fYzMJogu/qfD/ZDHvFWeUvFAGu6wHw/eQEMrLD0JgX5H0D3CZ0ki5VLL9zeRGJLy+vZ2RLMe82it9FhGI4foDICNnUX9kPDW7JF2P2e/EwA4mOVOi4+Ay2jL3dxkLELVcvli7WBMmZig/vQD9n8Qg+nbDmSdBwCuXl0F/YdyyYlsUEdq78C2FudSy3Tzw0WRqoYW2WakR63+dqRbSy+wI222A6nyPNXyV/kM0GLGjNW9rb2FlAhVp8WCbxKSN2ZZLIGBQQLwxQXNIrXnVT9/IC7fdQJ2aHDTTV6K0ZKkmsjS9gkVvduccsqqx4j5+NRT6AFW9t7T3FnW1THiCPc4qenYXORyBwKjFY6uw1dsOR3IGkq+D33yKVv6IreIokEfU+VJFmsGpiE9r7YEwiRAfsnOUXiXUm5uw7m6AOet2JANRrghItlfyQjiYwO08ZROZPoUy71qerd33h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DZYub6cG7zRCYzG9gVXyuWye7skk+SHroY/ZomuWT3DihZDJb9Ck8LVhd9L8scog21m2vJ9ImbIUPFyE17PaD8XsryCEk1gw+KGuhKV5dx5QuPTeGatZSEqyYke3458bKlDP4jL7U1EvE+OrGtxVWBF7U6sg2TK7Jqe9q6MVL5ZCdx+xrocEzXsgf0ECkBO2lgCqtzWyO4Z3b+/0RNrOPd6u63CQFOlZ0gdwhPrEwAgOeimogB5eEAjUGT/o3tNdwtm485GB6YvlmsdVxCm+w9FfcIx8IFh6H9+7jvG0w== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90c6fe17-09f8-40f2-c467-08dc585f1e5a X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2024 06:34:33.6406 (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: PR3P250MB0260 Subject: [FFmpeg-devel] [PATCH v3 01/27] 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: yrv6p5X4ATKY The API is similar to the ThreadFrame API, with the exception that it no longer has an included AVFrame and that it has its own mutexes and condition variables which makes it more independent of pthread_frame.c. 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. This will typically be solved by putting a ThreadProgress in a refcounted structure that is shared between threads. Signed-off-by: Andreas Rheinhardt --- Now using ff_mutex_*/ff_cond_* instead of their pthread counterparts. libavcodec/threadprogress.c | 79 ++++++++++++++++++++++++++++++++ libavcodec/threadprogress.h | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 libavcodec/threadprogress.c create mode 100644 libavcodec/threadprogress.h diff --git a/libavcodec/threadprogress.c b/libavcodec/threadprogress.c new file mode 100644 index 0000000000..62c4fd898b --- /dev/null +++ b/libavcodec/threadprogress.c @@ -0,0 +1,79 @@ +/* + * 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 + */ + +#include +#include + +#include "pthread_internal.h" +#include "threadprogress.h" +#include "libavutil/attributes.h" +#include "libavutil/thread.h" + +DEFINE_OFFSET_ARRAY(ThreadProgress, thread_progress, init, + (offsetof(ThreadProgress, progress_mutex)), + (offsetof(ThreadProgress, progress_cond))); + +av_cold int ff_thread_progress_init(ThreadProgress *pro, int init_mode) +{ + atomic_init(&pro->progress, init_mode ? -1 : INT_MAX); +#if HAVE_THREADS + if (init_mode) + return ff_pthread_init(pro, thread_progress_offsets); +#endif + pro->init = init_mode; + return 0; +} + +av_cold void ff_thread_progress_destroy(ThreadProgress *pro) +{ +#if HAVE_THREADS + ff_pthread_free(pro, thread_progress_offsets); +#else + pro->init = 0; +#endif +} + +void ff_thread_progress_report(ThreadProgress *pro, int n) +{ + if (atomic_load_explicit(&pro->progress, memory_order_relaxed) >= n) + return; + + atomic_store_explicit(&pro->progress, n, memory_order_release); + + ff_mutex_lock(&pro->progress_mutex); + ff_cond_broadcast(&pro->progress_cond); + ff_mutex_unlock(&pro->progress_mutex); +} + +void ff_thread_progress_await(const ThreadProgress *pro_c, int n) +{ + /* Casting const away here is safe, because we only read from progress + * and will leave pro_c in the same state upon leaving the function + * as it had at the beginning. */ + ThreadProgress *pro = (ThreadProgress*)pro_c; + + if (atomic_load_explicit(&pro->progress, memory_order_acquire) >= n) + return; + + ff_mutex_lock(&pro->progress_mutex); + while (atomic_load_explicit(&pro->progress, memory_order_relaxed) < n) + ff_cond_wait(&pro->progress_cond, &pro->progress_mutex); + ff_mutex_unlock(&pro->progress_mutex); +} diff --git a/libavcodec/threadprogress.h b/libavcodec/threadprogress.h new file mode 100644 index 0000000000..786802cbf1 --- /dev/null +++ b/libavcodec/threadprogress.h @@ -0,0 +1,91 @@ +/* + * 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 + +/** + * ThreadProgress is an API to easily notify other threads about progress + * of any kind as long as it can be packaged into an int and is consistent + * with the natural ordering of integers. + * + * Each initialized ThreadProgress can be in one of two modes: No-op mode + * or ordinary mode. In the former mode, ff_thread_report_progress() and + * ff_thread_await_progress() are no-ops to simply support usecases like + * non-frame-threading. Only in the latter case perform these functions + * what their name already implies. + */ + +#include +#include +#include "libavutil/thread.h" + +/** + * This struct should be treated as opaque by users. + */ +typedef struct ThreadProgress { + atomic_int progress; + unsigned init; + AVMutex progress_mutex; + AVCond progress_cond; +} ThreadProgress; + +/** + * Initialize a ThreadProgress. + * + * @param init_mode If zero, the ThreadProgress will be initialized so that + * to be in no-op mode as described above. Otherwise + */ +int ff_thread_progress_init(ThreadProgress *pro, int init_mode); + +/** + * Destroy a ThreadProgress. Can be called on a ThreadProgress that + * has never been initialized provided that the ThreadProgress struct + * has been initially zeroed. Must be called even if ff_thread_progress_init() + * failed. + */ +void ff_thread_progress_destroy(ThreadProgress *pro); + +/** + * Reset the ThreadProgress's progress progress counter; must only be called + * if it is not in use in any way (e.g. no thread may wait on it via + * ff_thread_progress_await()). + */ +static inline void ff_thread_progress_reset(ThreadProgress *pro) +{ + atomic_init(&pro->progress, pro->init ? -1 : INT_MAX); +} + +/** + * This function is a no-op in no-op mode; otherwise it notifies + * other threads that a certain level of progress has been reached. + * Later calls with lower values of progress have no effect. + */ +void ff_thread_progress_report(ThreadProgress *pro, int progress); + +/** + * This function is a no-op in no-op mode; otherwise it waits + * until other threads have reached a certain level of progress: + * This function will return after another thread has called + * ff_thread_progress_report() with the same or higher value for progress. + */ +void ff_thread_progress_await(const ThreadProgress *pro, int progress); + +#endif /* AVCODEC_THREADPROGRESS_H */