From patchwork Mon Apr 8 20:14:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47959 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3f98:b0:1a7:a0dc:8de5 with SMTP id ay24csp137125pzb; Mon, 8 Apr 2024 13:17:54 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVabpqDVqb08m9hFQBWxEE1xqr+arc0mMuGZA5FJKhW/sfkg8U1JlLguUWJcL4kJaKNG7pRnAAVUqBBcIbPGRxHa0Ff8g8C6BYDlQ== X-Google-Smtp-Source: AGHT+IEs8WFGJc+ezL4VC6E/rWmfP3h5w83bOuNm9x3uKoye08/J2P0dm5NN/IbCsO+RSCs9hIPX X-Received: by 2002:a05:6512:3106:b0:516:d30c:7236 with SMTP id n6-20020a056512310600b00516d30c7236mr8232912lfb.18.1712607474070; Mon, 08 Apr 2024 13:17:54 -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 b28-20020a19645c000000b00513d1aafc0dsi2419774lfj.171.2024.04.08.13.17.53; Mon, 08 Apr 2024 13:17:54 -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=kq6HJKUb; 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 4B82768D45D; Mon, 8 Apr 2024 23:16:20 +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-db8eur05olkn2058.outbound.protection.outlook.com [40.92.89.58]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 35A7668D433 for ; Mon, 8 Apr 2024 23:16:16 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iOgNmrvM/bU+ntAquCgjCdoX2rGmOJtlKJiTbV0e9P/c49hWDjziYR+zvMD8awd2/itFmvrUy3V3bGJRsB+VNSChw4tINLHbbvR/997zDwbymku/I5SFmpcoAuwabBzHDkoI0Ic6UfVbU80ubLNkOzqds+wK+yahA/3QS1RuorQGf68ieY2NUkCKh3HR/y+1h3kZHG+TDGyNZzUmjBP+5M4TUcOumvDjYsFANwp8ooW7lrmeg5ha186DD4yL1pPNgJVTfyPsvCOQ2mb80MtEkSC7lix/2h8hmOKG6DjlFpsrYo+rzyPwxeV+HfSGyKxeX2dG/gQ1OoFa4UlwO/GGIA== 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=wY5bzXq6wU/pEmcj5E01nJz3X+MnDM493jsjmNmcRnE=; b=NyOhcndFwFsirio9iu6Cidw4xomvYUtP4tu36Xt71i+g364AmMwHR56An3XPQbkR4wPP2eYObdBkzGozHUzsJ5ekRcIJJQj6LTWrjhcgaCq/eTHXY4qajxtoprQf1Qu4mJLvJfb/sYNic6jESmZp9RnMe3VR6/QEmSyzi0FfnLhGZj7gcNLh1c+rBih1bjhqX5RUfN6Fa+WNUbBhY8+T3pb3hvQi8M7Fq50mha/UL57ips3YXqkz3k33sfYJ0JTyF+oSoy9pNwA5VYAvtn8r+J365tsSsL8O5xSukmZAGvWnaEVsVfbQW3uewB7FOpFk+7gu9KXCBnDouh11MEqJLw== 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=wY5bzXq6wU/pEmcj5E01nJz3X+MnDM493jsjmNmcRnE=; b=kq6HJKUbr6p/SGaAlJSE+/7Gzq5uvTYqOW2jVaEIyOWKNQMpPIh0F0SDsGug5HKELKhzf01u9fuokeLZPPf/pK7Ptr+knVMX4dPhkGfoZJ+lhtObQnArarD2QT8uuUYoZ8psSy2s7cYyFXTc9IbyXIsAL+1Ypqsnx2l7XNEbQL6/T9eL/O5u5geza3FrYVrQpkQ2Y6vE5GB3j0EHtUNCb4yq2EuZsllEgPcdbolRfDUj8LR1Y29JogA2ErgYuBHj4KUB49u5QnseWsn9tiztgAEPZ3+i3lUp7K3VNEu3syXri2IKf6H6wqPW4jHi4xEKzczvj1OeZeaKOBrBiGrYLw== 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:15 +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:15 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Apr 2024 22:14:03 +0200 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [s9EkLI8e+B/N09PxAYLOerhR2qazrnRc9Bu1Zjws2+M=] 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-24-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: b377652b-d518-4c6b-8a1a-08dc5808bde6 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3w1gPfFXq0h+UTpgdv0CMyw2SWPS9dkJ86tQXm9c6VnB6+7pvWZVVdim9h05goEARPNsvn/qeaZvRHdXx2CY52BRshX9ZIgYd9Ld6cNaCIOBx4tYHMQFjPtxCkwBOfJEQH4O5c9NfrZEPX5Cdq5DKKU7XoCw2WoTnrdcQFRN13TUKOBlHcwL//+S00bAeGFhD2U7+ZCyl0tdr6MoP7H8m/2V6ygyG5yx+rtpbZI1PyLfPdaDvCauk+tt5R7Y2RffJqO4nup2wkOmqGaBOg4nUcYmQOSheJwvj7Tmz4nBFAwgg9nmfAY1aDB3tDRNW5M+MIQ44SSviFeN0R9znXktAhOG3o9xF7BAAZDjYfh0jNmNQqI5RL6GHpykWUPAdEogvz6FrfCFnm+SYd/Uhp5a7BSBbXXJbeDdp6zyt8X4EQ4YU+AR1h5eJ+iB+JQu4jZtjbacv2DpiEsTkVX2QED0bwdL4DsFXsX9tGqtN5igI9wBAXyfJOAr2qc/cleBqZJnFNmyaBgl+f6HL1k2YTe4kCAwGds59BThJUGisfG3nv519bihYQUDNCQGh4AYZpmv/jv4adP8vAtdmAaO7etg8sZxkXTqi5WyMsWlm7Vdj97E7hniNOJeV9Cb7TgxXEWV X-MS-Ex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X+NtZD2vBHNZE2zM60xSN4oTjIeaRrL40gRRu2o/c2u2ApfyezXQQl8GMcwdOwGYjdRznKuEYruivsN3Gv23y/B+urhnrEdX0arAM/eLXIQoR/5YGoKSBEpmsjcQZjObpfsVP5mNwFIqONqQc5ENoLECA0d706zNzpwfeyyqtLISocEzzsAN1121jMqneio0BuuWOsQDqDGChGcoUbbdPt4uBvLBawnRuOvvlq57kl72UnKvXyb5A5GwOzea6YLyI4EGngCtn0rW6Agy9g== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b377652b-d518-4c6b-8a1a-08dc5808bde6 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:15.0661 (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 25/27] avcodec/rkmppdec: Use RefStruct API for references to decoder itself 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: T3A54hCNcq9Z Avoids boilerplate code when creating the context and avoids allocations and therefore whole error paths when creating references to it. Also avoids an indirection and improves type-safety. Signed-off-by: Andreas Rheinhardt --- libavcodec/rkmppdec.c | 46 +++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 4e14d09c7c..e137e7e820 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -30,6 +30,7 @@ #include "codec_internal.h" #include "decode.h" #include "hwconfig.h" +#include "refstruct.h" #include "libavutil/buffer.h" #include "libavutil/common.h" #include "libavutil/frame.h" @@ -57,12 +58,12 @@ typedef struct { typedef struct { AVClass *av_class; - AVBufferRef *decoder_ref; + RKMPPDecoder *decoder; ///< RefStruct reference } RKMPPDecodeContext; typedef struct { MppFrame frame; - AVBufferRef *decoder_ref; + const RKMPPDecoder *decoder_ref; ///< RefStruct reference } RKMPPFrameContext; static MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx) @@ -90,7 +91,7 @@ static uint32_t rkmpp_get_frameformat(MppFrameFormat mppformat) static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, int64_t pts) { RKMPPDecodeContext *rk_context = avctx->priv_data; - RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + RKMPPDecoder *decoder = rk_context->decoder; int ret; MppPacket packet; @@ -125,13 +126,13 @@ static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, in static int rkmpp_close_decoder(AVCodecContext *avctx) { RKMPPDecodeContext *rk_context = avctx->priv_data; - av_buffer_unref(&rk_context->decoder_ref); + ff_refstruct_unref(&rk_context->decoder); return 0; } -static void rkmpp_release_decoder(void *opaque, uint8_t *data) +static void rkmpp_release_decoder(FFRefStructOpaque unused, void *obj) { - RKMPPDecoder *decoder = (RKMPPDecoder *)data; + RKMPPDecoder *decoder = obj; if (decoder->mpi) { decoder->mpi->reset(decoder->ctx); @@ -146,8 +147,6 @@ static void rkmpp_release_decoder(void *opaque, uint8_t *data) av_buffer_unref(&decoder->frames_ref); av_buffer_unref(&decoder->device_ref); - - av_free(decoder); } static int rkmpp_init_decoder(AVCodecContext *avctx) @@ -162,19 +161,13 @@ static int rkmpp_init_decoder(AVCodecContext *avctx) avctx->pix_fmt = AV_PIX_FMT_DRM_PRIME; // create a decoder and a ref to it - decoder = av_mallocz(sizeof(RKMPPDecoder)); + decoder = ff_refstruct_alloc_ext(sizeof(*decoder), 0, + NULL, rkmpp_release_decoder); if (!decoder) { ret = AVERROR(ENOMEM); goto fail; } - - rk_context->decoder_ref = av_buffer_create((uint8_t *)decoder, sizeof(*decoder), rkmpp_release_decoder, - NULL, AV_BUFFER_FLAG_READONLY); - if (!rk_context->decoder_ref) { - av_free(decoder); - ret = AVERROR(ENOMEM); - goto fail; - } + rk_context->decoder = decoder; av_log(avctx, AV_LOG_DEBUG, "Initializing RKMPP decoder.\n"); @@ -270,7 +263,7 @@ fail: static int rkmpp_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) { RKMPPDecodeContext *rk_context = avctx->priv_data; - RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + RKMPPDecoder *decoder = rk_context->decoder; int ret; // handle EOF @@ -312,7 +305,7 @@ static void rkmpp_release_frame(void *opaque, uint8_t *data) RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data; mpp_frame_deinit(&framecontext->frame); - av_buffer_unref(&framecontext->decoder_ref); + ff_refstruct_unref(&framecontext->decoder_ref); av_buffer_unref(&framecontextref); av_free(desc); @@ -321,7 +314,7 @@ static void rkmpp_release_frame(void *opaque, uint8_t *data) static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) { RKMPPDecodeContext *rk_context = avctx->priv_data; - RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + RKMPPDecoder *decoder = rk_context->decoder; RKMPPFrameContext *framecontext = NULL; AVBufferRef *framecontextref = NULL; int ret; @@ -449,11 +442,6 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) // MPP decoder needs to be closed only when all frames have been released. framecontext = (RKMPPFrameContext *)framecontextref->data; - framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref); - if (!framecontext->decoder_ref) { - ret = AVERROR(ENOMEM); - goto fail; - } framecontext->frame = mppframe; frame->data[0] = (uint8_t *)desc; @@ -464,6 +452,7 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) ret = AVERROR(ENOMEM); goto fail; } + framecontext->decoder_ref = ff_refstruct_ref(rk_context->decoder); frame->hw_frames_ctx = av_buffer_ref(decoder->frames_ref); if (!frame->hw_frames_ctx) { @@ -488,9 +477,6 @@ fail: if (mppframe) mpp_frame_deinit(&mppframe); - if (framecontext) - av_buffer_unref(&framecontext->decoder_ref); - if (framecontextref) av_buffer_unref(&framecontextref); @@ -503,7 +489,7 @@ fail: static int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame) { RKMPPDecodeContext *rk_context = avctx->priv_data; - RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + RKMPPDecoder *decoder = rk_context->decoder; int ret = MPP_NOK; AVPacket pkt = {0}; RK_S32 usedslots, freeslots; @@ -543,7 +529,7 @@ static int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame) static void rkmpp_flush(AVCodecContext *avctx) { RKMPPDecodeContext *rk_context = avctx->priv_data; - RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + RKMPPDecoder *decoder = rk_context->decoder; int ret = MPP_NOK; av_log(avctx, AV_LOG_DEBUG, "Flush.\n");