From patchwork Mon Apr 8 20:14:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47961 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3f98:b0:1a7:a0dc:8de5 with SMTP id ay24csp137244pzb; Mon, 8 Apr 2024 13:18:09 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU/SGZ4D4ty4ad46eHyI3BkY8FQeqxyc9fLyn3WAWOYiFWvpVdiL4zCngHB0pgz1QQF7iuPKxr/mi5BKWIQ5c7DVPgMcs2rx1d9WA== X-Google-Smtp-Source: AGHT+IGFpMj7XOr+PmfBsGrYhRsyyU34BvbPW/Xsj0+OB1jcgLPAUbA47+fUK2j+IMZj9tuC/nbA X-Received: by 2002:a50:bac6:0:b0:56e:bad:36b with SMTP id x64-20020a50bac6000000b0056e0bad036bmr6366158ede.21.1712607489645; Mon, 08 Apr 2024 13:18:09 -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 w11-20020a056402268b00b0056df1b8285asi3918997edd.654.2024.04.08.13.18.09; Mon, 08 Apr 2024 13:18:09 -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=B9c2yMao; 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 8AC2868D476; Mon, 8 Apr 2024 23:16:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2079.outbound.protection.outlook.com [40.92.89.79]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 94F7468D466 for ; Mon, 8 Apr 2024 23:16:21 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LZyZqYgCsBPqbPwZxplfeIhpE4fYho3jbMrJv5Ee2UB/s4LgAbmV8eflj0K1ItNkyMbW/25yGhytjBxEFJWMBivYayYGDJrMjc9Qx00sKD/2ZZf6fMgc0tdV0oX6xZi32GJrPW9Slv9VR9XMfbBzX2Mqe3LG0mE4tpRPGhE2C4THBBH4Wya/sDJXFEO3JMN6s2lQUiIEdw2H6lsSIu3OAJkXhe37LJolQXaNppTC59T5oyiQcF1piYxEIm5f8rLv5jye/z9ndkAGE+V6MWufrNtqDzu7+/PVnDzzjoTARWRyQ91kb72DH1ayPlj+u2VMbzbIvZv15Gjc8iv5wUE4Nw== 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=/MxzTsJx6gaw+drrNvTkS7xwCnUS/66RskecfEa9Yv4=; b=djTFZ/AnMJrO+QRXODg/ZIh0evNaa+0TTEeAzInm8MSmo51aF2k1p9tfnk4ne+bWr8NunTGnMadJ2ap+Pr3wQtkg9Vt2gRKKxrra/fXILLundMKb1Plr6Da4zvRfd70dgtZN3saE1HCOzvi1MEjkiaJyUg7h9DRGiPitv+bADoi4NbMIRfxqMsBoBsT8YAYWSE5ASGVxUNUkysmsljv8fna33P/LPKRA3BDEukZoUfbRqdwxjImpR3Pcu4fkLRI5xu+luwfPCwN9mmfD+/0PgKFRwqKOgornc1/4PkoE8CzUIGzURvZNT2ce+O3LV3tjdctCpsPOCF/zCzUPTuFW7A== 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=/MxzTsJx6gaw+drrNvTkS7xwCnUS/66RskecfEa9Yv4=; b=B9c2yMaoVTKpTEfbjOQ0HcvFvhClX4KLbskr8Nlt/KP8Cv1HdHAXkudDTRM85JreUXgnW9aRB1/dZTkkqE41aF2ox7K2oj9doNjoXxMQThplxld7j9rYwqIM6tNknwL27XbTHPm05mgwbz7kN3J85LZW6UuLlIYKY88vWh4hjbmuJEmsrBsrxB58rQGH3X5U22DyagqTOHLikv4yMpC8CkHZT4acOGtuLOVgYlh0JhanuJJ0rsMXlPc8aEgTqJbcnyeCiAhlOvFP1r1LtiPmvnEGB60cV5sXqSEkneW09IkEcowNsrHcDfLQfYmO4FWTKNCPRQY1zDY4LDTI3VNkMQ== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by AM8P250MB0171.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:322::18) 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 20:16:19 +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 20:16:19 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Apr 2024 22:14:05 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [89RToO9Hb64rxubprC1+uQF2f5+XI6HofbfQn9BY5ts=] X-ClientProxiedBy: ZR0P278CA0124.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:20::21) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240408201405.2136840-26-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|AM8P250MB0171:EE_ X-MS-Office365-Filtering-Correlation-Id: 84ef59ba-f5ec-42dc-3b36-08dc5808c092 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PLnapEz0ieyciUK+iRpa2vfigoexLCtD2aBtux+hYsE9yU0tmKv//bCo5XblPEzRoZxn0TRToisOYAHUT/BRWrgAWZiRkSXmGBlzk+TFEN3wLmsAQu+yF379g6juUMsO4kcwwnVRYe5IOzC8BRDcg+dg91jzY1l43VIzB9mDhQDTIGspKcpdFSqqjEwMJvEoyc0zv7x/RdxN6FDRK1aE8iEFOO1AXJj1R/SudMWbdsYd1qWq9I6JUBFKMMz9mVuBEkeqerPcKckRB94Zned45yotNqZR0R61PLr/mlUlHmTQyuiznj3n4DLPIZRqJb7kHShJUPgETbb5VcmD6t+gm8FNNmMqqP6JhfjjZHJj894HS+ik0WgNEOnrq+Vh12lm+GVOK5g0UQsnQJStjbJLqCC9reoFHEvYM3r7FJOM3W0EpNx/8vFX0kz8L953y8lmzPm7RfyFfNlmVD2LQpbdv2dLK3+EW3FpvN1Ohx7Wt+qrxdteiwMMYWnlHHxBivjMrnwcurXSg1xjXTrfjsGCbobpWrcz+Q3Q63yHE0JKAMKYMPmiYtauzRh8U/6LhcI8LNTUUPyVp4UC69yeVhsgFhON6t4AaXDciVILchNuvw88nJyZP//5I88EebfSnYDr X-MS-Exchange-AntiSpam-MessageData-ChunkCount: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PbJ+fQ4c9PVC52ynqMHDpb+93r1Y4GTLAcUoDAFkzsKBGU2qy7586HWU+TafdQ3mdzVgSIiNZmhjgzd+jObyuT56KZp8ctzNYHzdtpYyRZDt0iy6MdxjDveEYCCTOazbsObUyUO+4Ph/0oThG0Kk8c3M7WJr47UjxH6K3jrk7uVXnCdXRJ6gAlxPq8wjPr8vp809za4EQt2+7MtNRfvsdJMFkVwn6CH1RXdzPKOVp/NaN1IKSrllKstaifnh+NYzg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 84ef59ba-f5ec-42dc-3b36-08dc5808c092 X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2024 20:16:19.5550 (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: AM8P250MB0171 Subject: [FFmpeg-devel] [PATCH v2 27/27] avcodec/v4l2_(m2m|buffers): Use RefStruct API for context references 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: bWWS59yjN3cF Avoids allocations and therefore error checks; also avoids indirections and allows to remove the boilerplate code for creating an object with a dedicated free function. Signed-off-by: Andreas Rheinhardt --- This patch is also still untested. libavcodec/v4l2_buffers.c | 7 +++---- libavcodec/v4l2_buffers.h | 6 +++--- libavcodec/v4l2_m2m.c | 25 +++++++++---------------- libavcodec/v4l2_m2m.h | 5 ++--- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c index 2277135699..23474ee143 100644 --- a/libavcodec/v4l2_buffers.c +++ b/libavcodec/v4l2_buffers.c @@ -29,6 +29,7 @@ #include #include "libavcodec/avcodec.h" #include "libavutil/pixdesc.h" +#include "refstruct.h" #include "v4l2_context.h" #include "v4l2_buffers.h" #include "v4l2_m2m.h" @@ -229,7 +230,7 @@ static void v4l2_free_buffer(void *opaque, uint8_t *unused) ff_v4l2_buffer_enqueue(avbuf); } - av_buffer_unref(&avbuf->context_ref); + ff_refstruct_unref(&avbuf->context_ref); } } @@ -240,9 +241,7 @@ static int v4l2_buf_increase_ref(V4L2Buffer *in) if (in->context_ref) atomic_fetch_add(&in->context_refcount, 1); else { - in->context_ref = av_buffer_ref(s->self_ref); - if (!in->context_ref) - return AVERROR(ENOMEM); + in->context_ref = ff_refstruct_ref(s->self_ref); in->context_refcount = 1; } diff --git a/libavcodec/v4l2_buffers.h b/libavcodec/v4l2_buffers.h index 3d2ff1b9a5..e35b161309 100644 --- a/libavcodec/v4l2_buffers.h +++ b/libavcodec/v4l2_buffers.h @@ -28,7 +28,6 @@ #include #include -#include "libavutil/buffer.h" #include "libavutil/frame.h" #include "packet.h" @@ -46,8 +45,9 @@ typedef struct V4L2Buffer { struct V4L2Context *context; /* This object is refcounted per-plane, so we need to keep track - * of how many context-refs we are holding. */ - AVBufferRef *context_ref; + * of how many context-refs we are holding. + * This pointer is a RefStruct reference. */ + const struct V4L2m2mContext *context_ref; atomic_uint context_refcount; /* keep track of the mmap address and mmap length */ diff --git a/libavcodec/v4l2_m2m.c b/libavcodec/v4l2_m2m.c index ac086a7913..15415cfc4e 100644 --- a/libavcodec/v4l2_m2m.c +++ b/libavcodec/v4l2_m2m.c @@ -32,6 +32,7 @@ #include "libavutil/pixdesc.h" #include "libavutil/imgutils.h" #include "libavutil/pixfmt.h" +#include "refstruct.h" #include "v4l2_context.h" #include "v4l2_fmt.h" #include "v4l2_m2m.h" @@ -247,9 +248,9 @@ int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *s) return 0; } -static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) +static void v4l2_m2m_destroy_context(FFRefStructOpaque unused, void *context) { - V4L2m2mContext *s = (V4L2m2mContext*)context; + V4L2m2mContext *s = context; ff_v4l2_context_release(&s->capture); sem_destroy(&s->refsync); @@ -258,8 +259,6 @@ static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) close(s->fd); av_frame_free(&s->frame); av_packet_unref(&s->buf_pkt); - - av_free(s); } int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv) @@ -283,7 +282,7 @@ int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv) ff_v4l2_context_release(&s->output); s->self_ref = NULL; - av_buffer_unref(&priv->context_ref); + ff_refstruct_unref(&priv->context); return 0; } @@ -328,17 +327,11 @@ int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv) int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) { - *s = av_mallocz(sizeof(V4L2m2mContext)); + *s = ff_refstruct_alloc_ext(sizeof(**s), 0, NULL, + &v4l2_m2m_destroy_context); if (!*s) return AVERROR(ENOMEM); - priv->context_ref = av_buffer_create((uint8_t *) *s, sizeof(V4L2m2mContext), - &v4l2_m2m_destroy_context, NULL, 0); - if (!priv->context_ref) { - av_freep(s); - return AVERROR(ENOMEM); - } - /* assign the context */ priv->context = *s; (*s)->priv = priv; @@ -346,13 +339,13 @@ int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) /* populate it */ priv->context->capture.num_buffers = priv->num_capture_buffers; priv->context->output.num_buffers = priv->num_output_buffers; - priv->context->self_ref = priv->context_ref; + priv->context->self_ref = priv->context; priv->context->fd = -1; priv->context->frame = av_frame_alloc(); if (!priv->context->frame) { - av_buffer_unref(&priv->context_ref); - *s = NULL; /* freed when unreferencing context_ref */ + ff_refstruct_unref(&priv->context); + *s = NULL; /* freed when unreferencing context */ return AVERROR(ENOMEM); } diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h index 04d86d7b92..4ba33dc335 100644 --- a/libavcodec/v4l2_m2m.h +++ b/libavcodec/v4l2_m2m.h @@ -62,7 +62,7 @@ typedef struct V4L2m2mContext { AVFrame *frame; /* Reference to self; only valid while codec is active. */ - AVBufferRef *self_ref; + struct V4L2m2mContext *self_ref; /* reference back to V4L2m2mPriv */ void *priv; @@ -71,8 +71,7 @@ typedef struct V4L2m2mContext { typedef struct V4L2m2mPriv { AVClass *class; - V4L2m2mContext *context; - AVBufferRef *context_ref; + V4L2m2mContext *context; ///< RefStruct reference int num_output_buffers; int num_capture_buffers;