From patchwork Mon Apr 8 19:51:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47937 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3f98:b0:1a7:a0dc:8de5 with SMTP id ay24csp124206pzb; Mon, 8 Apr 2024 12:52:51 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWlaRc8oJLgXDue0jrgUhtTUvaEdWSFyN13v4Qkfqcsf6crHKHvvkLRG/46tGdaguGPd2W+o2RYMYp/JY7kvx3vVbSkenPKp8tTyQ== X-Google-Smtp-Source: AGHT+IFU0XXRQpcSjYvReKWwt7W+xKtpDyHFgEbFlK010T5z/hfwZ2pbTuNbtlG0Q6pxkPI8IrlZ X-Received: by 2002:a19:8c58:0:b0:516:d4bb:47fb with SMTP id i24-20020a198c58000000b00516d4bb47fbmr6215983lfj.1.1712605971595; Mon, 08 Apr 2024 12:52:51 -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 gx17-20020a170906f1d100b00a51ad5c4700si3448764ejb.433.2024.04.08.12.52.47; Mon, 08 Apr 2024 12:52:51 -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=omuO+4TR; 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 1148C68D305; Mon, 8 Apr 2024 22:52:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02olkn2092.outbound.protection.outlook.com [40.92.50.92]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 42F9D68D202 for ; Mon, 8 Apr 2024 22:52:37 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M4KkrSC79ZHkG/9DOTEAR0A57sU4sFUfXhGEXcpHZn3s4KX+l4SXjSTOUBBicTX+ARsxfbuCbcmGmx87TOJp8pobwB4KkhYTuHi6oY0UcrmOqGgFBUyrKXli271pI5trSeKCPAj4XMHqantfQ3vHmItLA+CevWyavPjyh/SU21Ge74dOOPskwrG91tjjA371wzviW0WXxLXXBBwe7tzy0gZ3n8NwwZab8POKkkzL7cWcxtPRB/5LVBGJVTdj5zAxvw9p7Wkqbhrl0PDUlYgYG7etTkpX/5SEeyDy9y/f7bKpuVY7E0sEQTnidwERKnL1DzuAq16WXoDymgFFANbswQ== 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=vWDONketteIUyWVGkxmxuwA30m0LVr9WWbAOERvkLoc=; b=kPvkKBZZ1ceHGxU71TBDFowTcVZ+8S5YHHMo8kHWapOuVNbFxDXLMuFLmZeDA1GIqDfGFhUDxQx4baEstnvHaRdTW2xXBjt/YcdOKG8hEhq2kCd/XLXzabv90HoXrRteSxvffHRJmt/by+asRQ3xQocELcHvybqW6+kwkGs3l9XYRganiqcEBOaowZ2GKpCdZ+Z2V3Cx4ihIvGpRL2k0AhQZFqejh6TooYlogT7/9DcUsQMPg/9QY/3sE/2fTb7OOzJMTQE5j9ZoNLVDfRmfRGKIPbd1QYIz5GDZyclPeK1EmLhE+dAc3zaYU34ya82+e6TKJlRTnicvoqxuDSQulg== 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=vWDONketteIUyWVGkxmxuwA30m0LVr9WWbAOERvkLoc=; b=omuO+4TRC7nuxf4fOE9px0iiELg4tldY17Jd6xzM9cN+Z0uaX2FJgZWkNC/zMxg2g/KJZBeV+93SwDX4W31XcgMUmPVbSO4Nuubc3Wz1xBVp77QW711W79q9QW9fP1fVJ+1houY737QKsW/8agSomEM4CM4IvspljX/846PSidX04rKocI3IPUPYQgAj1rJ/SQiQRGv/BFG7pDI6KHcUQnZ6Dpe1RtdP0+JE8D1YcRIIhRiv0gqF95FP+ibNwaxQN0MP2PFO6Qc/lyvzGxqnZUUzIoRGhJeIYGqI9BPmLxCPjnujQ/Gq8cmm6ll+uqDeT0Neg5hUnpJ6el6BXCSuwQ== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by DU0P250MB0457.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:345::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Mon, 8 Apr 2024 19:52: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; Mon, 8 Apr 2024 19:52:32 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Apr 2024 21:51:59 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 X-TMN: [6l7T92+I2hhCZYOCP/LceBM1a1FpuzLqbhrnyS03Dgs=] X-ClientProxiedBy: FR3P281CA0148.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:95::7) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240408195225.2136769-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|DU0P250MB0457:EE_ X-MS-Office365-Filtering-Correlation-Id: c7405960-0e22-4274-2bf6-08dc58056e3c X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EfxAPsnOk6X+ySViYihHnSxvVHde8RZVPrJYRDu7oe+vWezOQDuL9ZG0r08WrzyJExLatokHITS84+KSgqEuTk+U2d6TFIn9U9VwylTuJx+BJ1UcqecFcwPHq+SG5s5DyvLaWG3pIv7TZ6yWVWCFVaH8KirJJ0vIRAPQWVWU//0mp+BBnGo9fqhzfrrsNglHRpyrRM+Tw8HDypje9V73ZMlUoFk2G8NpR3J14Zj6wsksTgsyJD5cAjryUPgiuUCh28c8xuQg6ngJr2Ie9GmqashTaUBWi2vfSqvcoEVS+rRobdfNSRExcRsrhL96ivQLhm0UhajLjCmDJZF4hw9Xr5dNZzpVC3xWhjk52xFKt24ykSeOnl8ZqBzBQ6Muy3LmyFDV+OBCIeGaqXR9WHxKbA7uKjE5j7wtGp0pfj8YaTmAUR/xkOOFIcFXY5h5Gp2vVCm484UTwZ6soHGIkkS+HgC0nE4xWRumpUOCcvWfVKrud2j7vywmTMvSLlJLjqiwLQ0EOdaS/ry/BFyuoDCVRGhkFvB29xJDcK4zevst/ZSjiwUjbo5gZ8eu4lLVv2dDJwNBX7schIw49etA+SO6+qBuq22M+Ji7fBvyeBN/CiU= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-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4ynJ+nRH7fNExryUdqz2WQ0F8WO3Ob1/qnexy78g1m9daSzIxau17fo6P3hzUUQYNLny7d9KcizV6YKHWy7e8fkDuXupbRKi5E1mEvQiddYAZhOzkq0UAvUZuk8LP5sfDyEnOjYh1+XfoexeOqHokrzzCVl8u+t2F/h4ADOG/itXiSHKg9W6wXMizKmkAIL3LQ/Rp8j4rWGyFMakQCG3SwjQ2UgV4UGOb154OPmGA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: c7405960-0e22-4274-2bf6-08dc58056e3c X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2024 19:52:32.9203 (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: DU0P250MB0457 Subject: [FFmpeg-devel] [PATCH v2 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: kKQG0THWr9Y3 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 including its own mutex and condition variable. Hopefully no system has constraints on the number of mutexes and condition variables it supports. libavcodec/threadprogress.c | 73 +++++++++++++++++++++++++++++ libavcodec/threadprogress.h | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 164 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..dd2a4d2336 --- /dev/null +++ b/libavcodec/threadprogress.c @@ -0,0 +1,73 @@ +/* + * 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" + +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 (!init_mode) { + pro->init = 0; + return 0; + } + return ff_pthread_init(pro, thread_progress_offsets); +} + +av_cold void ff_thread_progress_destroy(ThreadProgress *pro) +{ + ff_pthread_free(pro, thread_progress_offsets); +} + +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); + + pthread_mutex_lock(&pro->progress_mutex); + pthread_cond_broadcast(&pro->progress_cond); + pthread_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; + + pthread_mutex_lock(&pro->progress_mutex); + while (atomic_load_explicit(&pro->progress, memory_order_relaxed) < n) + pthread_cond_wait(&pro->progress_cond, &pro->progress_mutex); + pthread_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 */