From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id C5CA542BC6 for ; Thu, 30 Jun 2022 21:48:40 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5C1C168B411; Fri, 1 Jul 2022 00:48:38 +0300 (EEST) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-oln040092066106.outbound.protection.outlook.com [40.92.66.106]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9CB2068ACBA for ; Fri, 1 Jul 2022 00:48:31 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VV1v2Z+5oSWjDm4jyQXk2NYeDX4A3/EYmoa5w+4ujApjnlv130mee40n9rW8Mut0Nr4CAs71xK5AteHj72+Ct1nOyTAtMecAyCdCtrb9v1UJI+ozdGXrChkmxSc3wjBZBJ1huGGGWvnjSXOX3CtvHaI8MJ9TJpz+kw9n8jNPzNrritNJcDwfPdnUxk6aEejfg97ncmFyJPHD7i1VLnxD0w5K/pq1MFCNg3oRPU+5Ybm3g3accWU6fQe7clDczf2iRaha8rX92JbV+Bs3nqy9z79EIw50OSKCzTIurrDzCumbWWuiq/5DTQgaRGGbl6/E/IRG95f6Ee18qGFQTuSYLQ== 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=DixF1T8ewAGu2sL2DSMvKcraNb75RNjtc6co5UaClrg=; b=YLVeStQg25/64p5ruTfYs9yJTG2OPbTUzxJvPztYXJbD0v4+AVvGcsCpaXoMh0u9BIamELngDxysj4410yBlPiwEbQECCggq14KFqcwtFym6MOYZxBDcIxKQCC7WjfurxPWy2glMlG47Yjvo/+Jdh8BuvTpgDsWwHmTtkanI3jDHsih4DzUSXWo88tGFRKn6OVxyGCFlsWvUWfFEOvieykUpEu1Epo86QzKGhcPhnyQ2b2NMZosOuHEu2qGweM7Qncu1v4uX6jxDYa5uSf7GiNL04Lqmhyuxt6CtUYHqqzIjkywoeXuKSFpkKiuRUuaH5T1pHaQHvMLqKN4ZnePL5w== 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=DixF1T8ewAGu2sL2DSMvKcraNb75RNjtc6co5UaClrg=; b=uldReGrIdGr3tXNomxn6JCvscVbWsUjE6tDTehW3r3eOkBnnm7kYbCL7tLO+HzlRoFCyUpKv3SkmvmWsT6JSh1FIQWe/cmBrQk+PBH32KZtzw0FujhfxCXkkap685ELGa+D0NlfE49VtghPg6031qSnk9MHVKmahpd20snaVpni2W/OUkCl91KQzExM7hABQn4SDg8hmljGz0R2Wp4+v5BITZLagudjFGcEDS/fUq1WHp2bQvuaCSP0yFqUK+zVMEud9BhDaq5N4yS91kLSXNdc1pw+faPxPat/lxKPqokFFcOqK6Wqzsxnef3qutv7q8UfQkLwNXVMtmApuDqbeZA== Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) by VI1PR01MB4333.eurprd01.prod.exchangelabs.com (2603:10a6:803:6a::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Thu, 30 Jun 2022 21:48:29 +0000 Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c]) by DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c%10]) with mapi id 15.20.5373.018; Thu, 30 Jun 2022 21:48:28 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 30 Jun 2022 23:48:00 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 X-TMN: [1cS5JxpUsWRa7zX6R/eSB40bBFfEhSM5] X-ClientProxiedBy: FR3P281CA0068.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:4b::15) To DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) X-Microsoft-Original-Message-ID: <20220630214817.1758325-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b0142c86-cef9-4a5f-f650-08da5ae2446f X-MS-Exchange-SLBlob-MailProps: gjx25WM8ZNXqMBxJfDcJWCkSlr5voV0aiiBKhBFLSANyrhN47j3KDJtVcEbryN1N4ceFVdVwTuhvE6SBPYKI+CT/zWf1U52BNXSf+d0BKR1zQLeTMTNvn7oMgW3fQD7JRrXcxSHx6NwNaksr249KkwV+bdpACfJmSu8MOpVnGhU1frDKM1uc8HZQEDUoMjjCIEK+1XC8JsurPXc5x/Xe4P29f1m29C5MQcQnkRak60xi73UCFZ+vZFbNPs0p3oVVOlG8iaGXO45xEguVmBWTHpgT+LUc6syQld9g3dZA3xcfBlvT8t2nBmfyM3K2Z2JFTZtFF0wWNOSjzNELuqNyLhupem/Sl24mmo7zUYwmonHslgrmsjKtcgDr1w5bIF2VD6oeAT5tEdzHpzOJsq4nnESZ1WDnGlbkWXCfjQoDtl2sv1LrtyMkVHtmBcPGGYw1lUVNLOgcz8xiyOllGnHaS6qiTssQLlmj6GHuf4U97NsSzpDqdu09E1Yg9Orq6xLDvDPJ1wkKEyFN37N+zUzD/V17OzrT6v4D884uagpyzUcYYVkrAZPQMWoLqtTADGYRZphBVSPQFFCZxS2m8ilUrLCkOA8nTCuVeT3zlDwczCCMO91LuY3++ejWPOqR5Vi+vLsQNS5+JAa5oq1lM1yYz0BUMLqtN4effnwGNqXrEppBhSCtjXXWqB42pJvDiT0DZf/9ulSOVFwXnek907d8iI7L8nptvCfdmpvcL3uydC4= X-MS-TrafficTypeDiagnostic: VI1PR01MB4333:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1/t7PnHzlVOIZfuSGCN2zO0kYhY0F8O4vddOh69uXOnUc1jcRoVXUJdbRZC9W14V+UUs6Zwh5vS83QhN4cnavl4u55h47NQvlOemDeOJKY+U/H93Ls8Ua/y3aOYzxYIwyPqkWQVmUsOzJ6wWpHM+AHHj9TPluEt6XwOuR7/zawptwQToJH2+5ZRbID+TSk2zsJx5rmpfw4MxJX7zsR6HFrJkBouyZwLkJm+2gFF3NTKx3VQBK3Nj8NlL+W3C7Hvo1oHzMiwTIOXUOP28Uv1fOFNLW3tN2n61OME1UYKZF9ww/NRtxEHM6JHIzwgMtRautIpKwJk6Tf8PQQzksTkW9xahIoRug4n8dawKmsRrjAUOHzOZ6EtCqJved1CzOEcT6Ev48qkyGkBw5NIRLO7MfpVZ58w884q9StMvn0hVRtEJw5H/uEDjxx/6j1hfpDjZvZx5fF+tZZpX86yb4gQ/U3oBxM1zvBRZAh+kwnuYRi5ffoM85hQeqPKJPsmDrlVlK8gGYNWRWULq2v1nI6rAnlLJXsx2UivOP94pIWH+qoV35EvYgGdS8TWGsDhYm09LR8DgvsqFnvE3/qqGZkBpvXngMJzPYY1Xo1vTTC8XNJWlEJYyRNZhNY0SdIuDsCcLD8aSAYYjdvdIml5BdSrQkvaMe8mNiPdPS5uM8NSuD5DftFFgl0IC8d1F4s5q2qR12cUI2/ne+15k3R7rB5cJbg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?bMzFazUvuCj9HCdqHdlPHcnwy9tXTfQ1qmOYQ7i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HsUYtW6w/M/4/H8k4zoZf8TyfDNFmxRndgpK6q5tY0l4oINGjQ+lV7aC?= =?us-ascii?Q?//QtLHq4hHlOzlF6te8D1MfzhTh7mqlYdsPI9ou9GkFeaGPemchMT3IpAZYt?= =?us-ascii?Q?S6gRfrYFaeHldYTYgxQrYPH8WGd28cnVgRgvW2MKitI3qsojgMiudvGch4Xe?= =?us-ascii?Q?tnH7WvhDS/bqqqd1dm2bVQG1pcFsFd7GcQ7YCEe5SmdlGADlAUbtuaxsCyd3?= =?us-ascii?Q?6PcCqepCCGjDa36KlnQcRXIryRReecKZCNDAsG8rzBbFU/nXL2S/1RoJJrvn?= =?us-ascii?Q?QPS/vk0GoAFH7THAU/3MRJ7avDnEt/8YsJYtCMDHrrFx+J8QKR+C2I9hkzA8?= =?us-ascii?Q?TRK3XNThh7OD11a0s92j5C2/2ATGiwUJGWfYfwDokrabtLbrSYj/4sPaNni1?= =?us-ascii?Q?Rak=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b0142c86-cef9-4a5f-f650-08da5ae2446f X-MS-Exchange-CrossTenant-AuthSource: DB6PR0101MB2214.eurprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2022 21:48:28.6455 (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: VI1PR01MB4333 Subject: [FFmpeg-devel] [PATCH v2 01/18] avcodec/pthread_slice: Don't reinitialise initialised mutex 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: It results in undefined behaviour. Instead initialize the mutexes and condition variables once during init (and check these initializations). Also combine the corresponding mutex and condition variable into one structure so that one can allocate their array jointly. Signed-off-by: Andreas Rheinhardt --- Now also adding the fallback function in case threads are disabled. libavcodec/hevcdec.c | 7 +++- libavcodec/pthread_slice.c | 83 ++++++++++++++++++++++++-------------- libavcodec/thread.h | 1 + libavcodec/utils.c | 5 +++ 4 files changed, 63 insertions(+), 33 deletions(-) diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c index 7037048d53..f222f20706 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -3803,9 +3803,12 @@ static av_cold int hevc_decode_init(AVCodecContext *avctx) HEVCContext *s = avctx->priv_data; int ret; - if(avctx->active_thread_type & FF_THREAD_SLICE) + if (avctx->active_thread_type & FF_THREAD_SLICE) { s->threads_number = avctx->thread_count; - else + ret = ff_slice_thread_init_progress(avctx); + if (ret < 0) + return ret; + } else s->threads_number = 1; if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1) diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c index 0ad1965a22..e34116116d 100644 --- a/libavcodec/pthread_slice.c +++ b/libavcodec/pthread_slice.c @@ -41,6 +41,11 @@ typedef int (action_func)(AVCodecContext *c, void *arg); typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr); typedef int (main_func)(AVCodecContext *c); +typedef struct Progress { + pthread_cond_t cond; + pthread_mutex_t mutex; +} Progress; + typedef struct SliceThreadContext { AVSliceThread *thread; action_func *func; @@ -53,8 +58,7 @@ typedef struct SliceThreadContext { int *entries; int entries_count; int thread_count; - pthread_cond_t *progress_cond; - pthread_mutex_t *progress_mutex; + Progress *progress; } SliceThreadContext; static void main_function(void *priv) { @@ -83,13 +87,13 @@ void ff_slice_thread_free(AVCodecContext *avctx) avpriv_slicethread_free(&c->thread); for (i = 0; i < c->thread_count; i++) { - pthread_mutex_destroy(&c->progress_mutex[i]); - pthread_cond_destroy(&c->progress_cond[i]); + Progress *const progress = &c->progress[i]; + pthread_mutex_destroy(&progress->mutex); + pthread_cond_destroy(&progress->cond); } av_freep(&c->entries); - av_freep(&c->progress_mutex); - av_freep(&c->progress_cond); + av_freep(&c->progress); av_freep(&avctx->internal->thread_ctx); } @@ -172,65 +176,82 @@ int ff_slice_thread_init(AVCodecContext *avctx) return 0; } +int av_cold ff_slice_thread_init_progress(AVCodecContext *avctx) +{ + SliceThreadContext *const p = avctx->internal->thread_ctx; + int err, i = 0, thread_count = avctx->thread_count; + + p->progress = av_calloc(thread_count, sizeof(*p->progress)); + if (!p->progress) { + err = AVERROR(ENOMEM); + goto fail; + } + + for (; i < thread_count; i++) { + Progress *const progress = &p->progress[i]; + err = pthread_mutex_init(&progress->mutex, NULL); + if (err) { + err = AVERROR(err); + goto fail; + } + err = pthread_cond_init (&progress->cond, NULL); + if (err) { + err = AVERROR(err); + pthread_mutex_destroy(&progress->mutex); + goto fail; + } + } + err = 0; +fail: + p->thread_count = i; + return err; +} + void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n) { SliceThreadContext *p = avctx->internal->thread_ctx; + Progress *const progress = &p->progress[thread]; int *entries = p->entries; - pthread_mutex_lock(&p->progress_mutex[thread]); + pthread_mutex_lock(&progress->mutex); entries[field] +=n; - pthread_cond_signal(&p->progress_cond[thread]); - pthread_mutex_unlock(&p->progress_mutex[thread]); + pthread_cond_signal(&progress->cond); + pthread_mutex_unlock(&progress->mutex); } void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift) { SliceThreadContext *p = avctx->internal->thread_ctx; + Progress *progress; int *entries = p->entries; if (!entries || !field) return; thread = thread ? thread - 1 : p->thread_count - 1; + progress = &p->progress[thread]; - pthread_mutex_lock(&p->progress_mutex[thread]); + pthread_mutex_lock(&progress->mutex); while ((entries[field - 1] - entries[field]) < shift){ - pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]); + pthread_cond_wait(&progress->cond, &progress->mutex); } - pthread_mutex_unlock(&p->progress_mutex[thread]); + pthread_mutex_unlock(&progress->mutex); } int ff_alloc_entries(AVCodecContext *avctx, int count) { - int i; - if (avctx->active_thread_type & FF_THREAD_SLICE) { SliceThreadContext *p = avctx->internal->thread_ctx; if (p->entries) { - av_assert0(p->thread_count == avctx->thread_count); av_freep(&p->entries); } - p->thread_count = avctx->thread_count; p->entries = av_calloc(count, sizeof(*p->entries)); - - if (!p->progress_mutex) { - p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t)); - p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t)); - } - - if (!p->entries || !p->progress_mutex || !p->progress_cond) { - av_freep(&p->entries); - av_freep(&p->progress_mutex); - av_freep(&p->progress_cond); + if (!p->entries) { + p->entries_count = 0; return AVERROR(ENOMEM); } p->entries_count = count; - - for (i = 0; i < p->thread_count; i++) { - pthread_mutex_init(&p->progress_mutex[i], NULL); - pthread_cond_init(&p->progress_cond[i], NULL); - } } return 0; diff --git a/libavcodec/thread.h b/libavcodec/thread.h index d36dc83bd7..92cbd927f1 100644 --- a/libavcodec/thread.h +++ b/libavcodec/thread.h @@ -106,6 +106,7 @@ int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, void ff_thread_free(AVCodecContext *s); int ff_alloc_entries(AVCodecContext *avctx, int count); void ff_reset_entries(AVCodecContext *avctx); +int ff_slice_thread_init_progress(AVCodecContext *avctx); void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n); void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift); diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 940f25fe7a..f78475d0ad 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -958,6 +958,11 @@ int ff_thread_can_start_frame(AVCodecContext *avctx) return 1; } +int ff_slice_thread_init_progress(AVCodecContext *avctx) +{ + return 0; +} + int ff_alloc_entries(AVCodecContext *avctx, int count) { return 0; -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".