From patchwork Tue Sep 19 19:56:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 43814 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a886:b0:149:dfde:5c0a with SMTP id ca6csp185371pzb; Tue, 19 Sep 2023 12:57:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHuZdYNiCy/C/X5qu8W2OArarMR/7YziSBuKgf/AptZnDBmwyx/Bv2nB8O2hZjGMhk7ahZQ X-Received: by 2002:a5d:4a11:0:b0:31f:fe07:fde7 with SMTP id m17-20020a5d4a11000000b0031ffe07fde7mr491840wrq.1.1695153422074; Tue, 19 Sep 2023 12:57:02 -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 a6-20020a17090640c600b0098cf3eaee47si11316443ejk.689.2023.09.19.12.57.01; Tue, 19 Sep 2023 12:57:02 -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=s8rTfWQS; 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 4FE7A68C843; Tue, 19 Sep 2023 22:56:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2101.outbound.protection.outlook.com [40.92.91.101]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C305D68C63C for ; Tue, 19 Sep 2023 22:56:46 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d9t1nm6hUdo2FXmoQX+R6gXrKlLJYS7L9gzvxH/raaYW83KeROAbvWJY8MESomKBJySfgqD1mrGWmpMW1uW7cT6gLN8RBbcqkAptAqiHx8jQ4h7tKJNUQFONDX+ojO0apdlf0TiCE3yyHPwhYpypXN89cAbAUfHZN2b8My1OagDmMjqmIMLicaWj45dIezopB1V82D3vB4JK1tGBRlzwwsOWQ44nt/X84DkF0nrVJ4BG3jJ+DxBff1/ATZl+JDkPFCjfDWTOGE5fOTGoQx77TrZ4xWxqYtibL3mv3ENCzat+3gxiwFYOo3mf+E1V3U5FcFQei0YNn62aPFUC/RBKZQ== 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=ytaasbqJ0RzrauE76QkDEgFflRWfwI4v4QT9PnKRWNY=; b=mKhbGuoTe0oHwVQh/AbsQb1uEoFDOPdJ6unWePwHISgkOMsWtahOsgP8B0W6hfN7A/vtXuRDSiPNz1wMpAEGI5n+AGQCxdgGuUmDCoDQC9SX0L3LDs9dsqcW6koIgjfWsHZY4cXXi4Hb/IwQPoAfi9W+nn68Tk423XbccwsSEQGR9IOkdkNIPKhhyYCvpMve1i1ciF64aEWxnqz+sCZ47UkA9z11uPe81JaLUr3DKf8QCy+ZWIjqBqrkOe+LN3Vhmkt5FBglaBlRJKNPtawaMawSfhzDd+ZpAXksA2UcgqFH959a8IHGRXyXBntSpDPFqV5XJU//UINhhzZgBJmkGQ== 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=ytaasbqJ0RzrauE76QkDEgFflRWfwI4v4QT9PnKRWNY=; b=s8rTfWQSU/9O3Q6wpppKKG+p+3fDKhWP3jdSVperqocZb/ZwmU1ApJRn8W5M0MR7grDnzOwY4/XTtMJwCDndCAlK8guobLuPLp9wRC7Ieo6Hm1W1C1U+m9mgqo5xD1DrqhEriUxUc1vAexdnbpH9XmIH7nrJqIVRkMJ6kLWOIlP/vO2dMg5IzZmW+T4PCBvD+AB3TIY8OOR2GGBis4YaqGa7LvLIhR//dq4NfodNIhy0k133618mpWPVYke9iA+Ki2cInGKG/UompiZWV+Ob4Wv1Z1cv9d5gon1oulqrtDukUUWKecOdI1V86uckuARWW5vsGND70Ryg1hSXe54eIA== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by DU2P250MB0303.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:27c::7) 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:56:45 +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:56:45 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Sep 2023 21:56:55 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [8VswBUT6/gajMM1BF8RYOQ6FLiXAJ4+u] 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-3-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|DU2P250MB0303:EE_ X-MS-Office365-Filtering-Correlation-Id: 01b2f255-1518-458d-99f3-08dbb94a8d41 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rkhZBNAc4GrUjpi/9YWYv8kGGovRFChRqPFn8yF1sMfDmBWKn+th8V7xfGXIsiptPrkcb5AQ+OaR5B5bMxjDLZt+KVUtHGEdJLRxAV81VExPxeIhm4zT5qiJJuin5UMaw2YiWgWGavbJZpULJJTjx5dZ5uBLYnnp7XigpkFQNbUOG2K8AyRbnrgk8Sy6t8jgaupW6JQ6BeHfFyxuKaeQR2a4NZPXZ4Uek6CIi13KAsnF0jjTfyS0YWtDAu+rxtuqIDZOCJ/M9p4sQMC7B0Ube5cDKFQe8S0cThf+wDsP/vJz+HbKGINf9edDxVBPIf2mT562PjXpP0D0HRjdg+j+thX37Q8LNJN2L2RQZX0Hn783AN+6AxWb6xwenW6m13+qxXW0ZJ9tLftgdYcwMSQP3zghGQY5xBnGRdKid8tdrzuSWee3MqHdU1MvGQHJax3s2lT4+7RVZ4Gc+2V2hjP+XBDaSIlL3q0E3xU1N17SEh2jvo7+jHabFQxrX8zqMhhCK2p8t5+5zsOo+pZSY3cGjQY9owYXMp5yPBoHrWxqVU7vZTDpJh43EsISNyxcQ6tq5YBUQuVQ1qZsj3Xn2xM1NQ9ac9yZapowb9veKjDQZyk1XHIh1bDf9rYUXqtqN30Z X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uCXsjXfAvY2t+EEdKmNvCju/h8yxQBv+aO5iQf2eO6IzXA2PL/VA5lH2IoQIzCeWAcShFR/gs/Dn9ffJUbF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QnRNjsSrqDnBQrAj6QxKu4jH1mT3joSDe6SKmoll8Au4stcLvOqzyovv6pGvs8wBjWwy7KcyiS9k/icCFgPgofaUL+wfMv+/BMyw7/GTVAR2+GOFinRpx X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 01b2f255-1518-458d-99f3-08dbb94a8d41 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:56:45.4106 (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: DU2P250MB0303 Subject: [FFmpeg-devel] [PATCH 03/42] avcodec/get_buffer: Use RefStruct API for FramePool 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: 45CfHniJH/CD Avoids allocations and frees and error checks for said allocations; also avoids a few indirections and casts. Signed-off-by: Andreas Rheinhardt --- libavcodec/avcodec.c | 3 ++- libavcodec/get_buffer.c | 44 ++++++++++---------------------------- libavcodec/internal.h | 2 +- libavcodec/pthread_frame.c | 7 +++--- 4 files changed, 17 insertions(+), 39 deletions(-) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 131834b6de..6365ab87a6 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -43,6 +43,7 @@ #include "frame_thread_encoder.h" #include "hwconfig.h" #include "internal.h" +#include "refstruct.h" #include "thread.h" /** @@ -458,7 +459,7 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_frame_free(&avci->in_frame); av_frame_free(&avci->recon_frame); - av_buffer_unref(&avci->pool); + ff_refstruct_unref(&avci->pool); ff_hwaccel_uninit(avctx); diff --git a/libavcodec/get_buffer.c b/libavcodec/get_buffer.c index a04fd878de..647f8a3df7 100644 --- a/libavcodec/get_buffer.c +++ b/libavcodec/get_buffer.c @@ -32,6 +32,7 @@ #include "avcodec.h" #include "internal.h" +#include "refstruct.h" typedef struct FramePool { /** @@ -52,40 +53,18 @@ typedef struct FramePool { int samples; } FramePool; -static void frame_pool_free(void *opaque, uint8_t *data) +static void frame_pool_free(FFRefStructOpaque unused, void *obj) { - FramePool *pool = (FramePool*)data; + FramePool *pool = obj; int i; for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++) av_buffer_pool_uninit(&pool->pools[i]); - - av_freep(&data); -} - -static AVBufferRef *frame_pool_alloc(void) -{ - FramePool *pool = av_mallocz(sizeof(*pool)); - AVBufferRef *buf; - - if (!pool) - return NULL; - - buf = av_buffer_create((uint8_t*)pool, sizeof(*pool), - frame_pool_free, NULL, 0); - if (!buf) { - av_freep(&pool); - return NULL; - } - - return buf; } static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame) { - FramePool *pool = avctx->internal->pool ? - (FramePool*)avctx->internal->pool->data : NULL; - AVBufferRef *pool_buf; + FramePool *pool = avctx->internal->pool; int i, ret, ch, planes; if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { @@ -109,10 +88,9 @@ FF_ENABLE_DEPRECATION_WARNINGS return 0; } - pool_buf = frame_pool_alloc(); - if (!pool_buf) + pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free); + if (!pool) return AVERROR(ENOMEM); - pool = (FramePool*)pool_buf->data; switch (avctx->codec_type) { case AVMEDIA_TYPE_VIDEO: { @@ -189,18 +167,18 @@ FF_ENABLE_DEPRECATION_WARNINGS default: av_assert0(0); } - av_buffer_unref(&avctx->internal->pool); - avctx->internal->pool = pool_buf; + ff_refstruct_unref(&avctx->internal->pool); + avctx->internal->pool = pool; return 0; fail: - av_buffer_unref(&pool_buf); + ff_refstruct_unref(&pool); return ret; } static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame) { - FramePool *pool = (FramePool*)avctx->internal->pool->data; + FramePool *pool = avctx->internal->pool; int planes = pool->planes; int i; @@ -245,7 +223,7 @@ fail: static int video_get_buffer(AVCodecContext *s, AVFrame *pic) { - FramePool *pool = (FramePool*)s->internal->pool->data; + FramePool *pool = s->internal->pool; int i; if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) { diff --git a/libavcodec/internal.h b/libavcodec/internal.h index 83e0bc3fb2..eb9e0d707c 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -62,7 +62,7 @@ typedef struct AVCodecInternal { */ int pad_samples; - AVBufferRef *pool; + struct FramePool *pool; void *thread_ctx; diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index 138576778d..7e274b0559 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -35,6 +35,7 @@ #include "hwconfig.h" #include "internal.h" #include "pthread_internal.h" +#include "refstruct.h" #include "thread.h" #include "threadframe.h" #include "version_major.h" @@ -329,9 +330,7 @@ FF_ENABLE_DEPRECATION_WARNINGS dst->hwaccel_flags = src->hwaccel_flags; - err = av_buffer_replace(&dst->internal->pool, src->internal->pool); - if (err < 0) - return err; + ff_refstruct_replace(&dst->internal->pool, src->internal->pool); } if (for_user) { @@ -740,7 +739,7 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) av_freep(&ctx->priv_data); } - av_buffer_unref(&ctx->internal->pool); + ff_refstruct_unref(&ctx->internal->pool); av_packet_free(&ctx->internal->last_pkt_props); av_freep(&ctx->internal); av_buffer_unref(&ctx->hw_frames_ctx);