From patchwork Tue Sep 19 19:57:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 43830 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a886:b0:149:dfde:5c0a with SMTP id ca6csp186851pzb; Tue, 19 Sep 2023 13:00:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFR/FyCLg8svuVZlXQf+bR5RwvCLnJFkyXzlADP412SQhSwuk78KmwVLkaDfGu2uP7E5r7Z X-Received: by 2002:a5d:438f:0:b0:321:6c90:55f7 with SMTP id i15-20020a5d438f000000b003216c9055f7mr495810wrq.18.1695153623844; Tue, 19 Sep 2023 13:00:23 -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 lf28-20020a170907175c00b0099d804da2ebsi10031709ejc.503.2023.09.19.13.00.22; Tue, 19 Sep 2023 13:00:23 -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=afb71KMd; 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 884E268C9A7; Tue, 19 Sep 2023 22:58:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01olkn2072.outbound.protection.outlook.com [40.92.65.72]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C9CAA68C9A3 for ; Tue, 19 Sep 2023 22:58:09 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OC8HgFBuuECWu+/jdoPgpvdgmG8v6BGSY1IOb4GHHP1fzF83CccEntbxSeFUMWv09sAapl7SPQ6sIcL45xDBBJ9CSfZjWnaa5rXWeH05yKgLDVj3ugeMNDxZDGcTLYhTNkpSubueAG4BQAI/XMEl605lIX8Nsn7GhcMh0oGT6G/cpyyePSvMCQHl2HBLAgcgKnMuZLmIZvzRhMlYZHLjiFsv07/hrZkb42zAIw0Ldb6cHRs1Y5Wk7lO1DPMUID9/O2vvDk1Q25e1Uzpv5CIussIHc32ghHtGDuVWcu347du9KYv8gHNReFs3E615gLLVIyhMzDFEAjkH8Q2XGVd9ow== 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=S4qlXMBvQQJbLyfSbhfP9quennPwRZLGAOa9OYNZp0U=; b=eGzbCtNtJtHbAxBD1QAcVPsp/+VRx67YkdX1Yau8m7yLcp1Ebcb1ShmITTx9DVy2VF8PMDRTirjB94DSzJwo260Gn6yipJF2J+EKLimmjZmpESnzGv8FQdhNqV036s4WTsI0oeBiq5F0aZhKo+RTxkydM8urfVaflLa0m/yvX+9wgUY8c1QpYig+Z5IsVEPSW14ExtOJ6XzyNU7wiMvfyYJEXSAODyl8YC0CUD6K8uTvTaNugzJNrJ5ZuAbXOINLt1k5au518QlyPsv1qIIP+U17Cb+UipfUZxkT6prcCN+SJ+s8Y1vOpU22xbg9yFuOueJOgsl6BBOuvJJYaTUOyA== 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=S4qlXMBvQQJbLyfSbhfP9quennPwRZLGAOa9OYNZp0U=; b=afb71KMdHB+xvXc02zhmT20GzBlsQssbSwhfb9ArppJuM8II45x3JGA/a+5zY8RX+xpQWu4nqhSAlWw7dwAsljqcVYd7xx6JyMpBp4vpX7NBHKzcTyDf7/B4TVR4HHWo/fiZK/hEKF9L7XdA8hzYrEX2bjcvSRvhSV8Sdr8G1fw0ZlMdK2OxryTg9G09UmLG7GBVhBYJVjdppks/IF0yU2+fT8CtEC0tTeogUSHNmXRy3GlygL75dYGmuP8KZ84IVUG0IDWJN6MgdHZtZLuKxA7Lxz4KdeLN3AUXdgTKop0dtlQfuK2sPCJ8HM1Uoq3v3QX7X8d4j7+FCQFoJRFhnw== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by DU2P250MB0237.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:276::14) 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:58:08 +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:58:07 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Sep 2023 21:57:15 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [54NHDliJayrXfUCN1TBdnoG0FHd+6xhx] 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-23-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|DU2P250MB0237:EE_ X-MS-Office365-Filtering-Correlation-Id: 3e4d98bc-4693-4dd5-ac01-08dbb94abe58 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CGZgzHFFf46f4kRjursi/vaE4sl5dEsKC+HIWgRMexiZJgPDCluJfop5x8JjOkzwMLcLntvG/8bV5+QKjTogUybrX2cxJk66/NMIawH55ZV/VIC4MXGN6qckxV9LTuoRYBqkc12c6uc5dFmrk6eGO9cFrjbIEEkQEtgx0GObG/yas4Bo/sdUsf25FMaIoGZ+fhMOORlKfhBMrwVK37GQfS9LvelSdP/nCqbG0agXWG8ebKy8s+BjZO9ehvAYLVijYDOSQqRc68JEgdRNFZwIbZZwt1SnD2WF+pLfjZ9cHPZe0tKhEh3g2NbrAJ9rvYdeeFSWTQ30l5/CdHs9GHiD1jXOknOv+6Q01QEKQtH1g+A3SzfS2TPx6nqobWZnwEOw03bMSh6rMHKMb4FtrqM2KM3nhAd/4YnTfJ62aLCVNbNtaoZieaBtO67ODV0G2wlrsDMzoQ4/8D5hKJxDFPK6JIFa379Im+VovpWHBLcFCDfQIA6EhG8UWypFe2cs2aEv6kzvB0x7NIC27A4h6hefP+N8m9kHTE9zYlNe8U2o2+FPzVARJdsedubOUoF7wImT6qUWnCXK3YIcEauYKa81edNhT3hbQrJQDIxWKUb44HL5qQFanH0Qr9RKDgLlbzPt X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OUGXtDaIKmmz8+lL2lXEay1ZO7wKQgOyBTPtPGX9VOYsqsKwdzgsr0LyZMt4eiHbMnOAXrC0GBd0t/yyhpYNnBZlGwDF+dX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3brBuYaJ8BYaztrWRRpdmAez4AiybB6i250VrNjMZ3/H/FcRf4Fu0vQbIGFC0mLJ23fMaGzzDql/6yDiczD9ojjDT+BkudOv9BabXcfNrB06txmWCj59zy X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3e4d98bc-4693-4dd5-ac01-08dbb94abe58 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:58:07.7541 (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: DU2P250MB0237 Subject: [FFmpeg-devel] [PATCH 23/42] avcodec/vaapi_encode: Use RefStruct pool API, stop abusing AVBuffer API 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: nwbdivirtcPA Up until now, the VAAPI encoder uses fake data with the AVBuffer-API: The data pointer does not point to real memory, but is instead just a VABufferID converted to a pointer. This has probably been copied from the VAAPI-hwcontext-API (which presumably does it to avoid allocations). This commit changes this without causing additional allocations by switching to the RefStruct-pool API. Signed-off-by: Andreas Rheinhardt --- libavcodec/vaapi_encode.c | 60 ++++++++++++++++----------------------- libavcodec/vaapi_encode.h | 5 ++-- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index 0316fe5c18..3f06d7f21e 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -16,11 +16,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "config_components.h" - #include #include +#include "config.h" + #include "libavutil/avassert.h" #include "libavutil/common.h" #include "libavutil/internal.h" @@ -30,6 +30,7 @@ #include "vaapi_encode.h" #include "encode.h" #include "avcodec.h" +#include "refstruct.h" const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = { HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI), @@ -309,12 +310,12 @@ static int vaapi_encode_issue(AVCodecContext *avctx, pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3]; av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface); - pic->output_buffer_ref = av_buffer_pool_get(ctx->output_buffer_pool); + pic->output_buffer_ref = ff_refstruct_pool_get(ctx->output_buffer_pool); if (!pic->output_buffer_ref) { err = AVERROR(ENOMEM); goto fail; } - pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data; + pic->output_buffer = *pic->output_buffer_ref; av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n", pic->output_buffer); @@ -645,7 +646,7 @@ fail_at_end: av_freep(&pic->slices); av_freep(&pic->roi); av_frame_free(&pic->recon_image); - av_buffer_unref(&pic->output_buffer_ref); + ff_refstruct_unref(&pic->output_buffer_ref); pic->output_buffer = VA_INVALID_ID; return err; } @@ -713,7 +714,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, pic->opaque_ref = NULL; } - av_buffer_unref(&pic->output_buffer_ref); + ff_refstruct_unref(&pic->output_buffer_ref); pic->output_buffer = VA_INVALID_ID; av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n", @@ -723,7 +724,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, fail_mapped: vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer); fail: - av_buffer_unref(&pic->output_buffer_ref); + ff_refstruct_unref(&pic->output_buffer_ref); pic->output_buffer = VA_INVALID_ID; return err; } @@ -738,7 +739,7 @@ static int vaapi_encode_discard(AVCodecContext *avctx, "%"PRId64"/%"PRId64".\n", pic->display_order, pic->encode_order); - av_buffer_unref(&pic->output_buffer_ref); + ff_refstruct_unref(&pic->output_buffer_ref); pic->output_buffer = VA_INVALID_ID; } @@ -2420,28 +2421,25 @@ static av_cold int vaapi_encode_init_roi(AVCodecContext *avctx) return 0; } -static void vaapi_encode_free_output_buffer(void *opaque, - uint8_t *data) +static void vaapi_encode_free_output_buffer(FFRefStructOpaque opaque, + void *obj) { - AVCodecContext *avctx = opaque; + AVCodecContext *avctx = opaque.nc; VAAPIEncodeContext *ctx = avctx->priv_data; - VABufferID buffer_id; - - buffer_id = (VABufferID)(uintptr_t)data; + VABufferID *buffer_id_ref = obj; + VABufferID buffer_id = *buffer_id_ref; vaDestroyBuffer(ctx->hwctx->display, buffer_id); av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id); } -static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque, - size_t size) +static int vaapi_encode_alloc_output_buffer(FFRefStructOpaque opaque, void *obj) { - AVCodecContext *avctx = opaque; + AVCodecContext *avctx = opaque.nc; VAAPIEncodeContext *ctx = avctx->priv_data; - VABufferID buffer_id; + VABufferID *buffer_id = obj; VAStatus vas; - AVBufferRef *ref; // The output buffer size is fixed, so it needs to be large enough // to hold the largest possible compressed frame. We assume here @@ -2450,25 +2448,16 @@ static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque, vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, VAEncCodedBufferType, 3 * ctx->surface_width * ctx->surface_height + - (1 << 16), 1, 0, &buffer_id); + (1 << 16), 1, 0, buffer_id); if (vas != VA_STATUS_SUCCESS) { av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream " "output buffer: %d (%s).\n", vas, vaErrorStr(vas)); - return NULL; + return AVERROR(ENOMEM); } - av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id); + av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", *buffer_id); - ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id, - sizeof(buffer_id), - &vaapi_encode_free_output_buffer, - avctx, AV_BUFFER_FLAG_READONLY); - if (!ref) { - vaDestroyBuffer(ctx->hwctx->display, buffer_id); - return NULL; - } - - return ref; + return 0; } static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx) @@ -2677,8 +2666,9 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) } ctx->output_buffer_pool = - av_buffer_pool_init2(sizeof(VABufferID), avctx, - &vaapi_encode_alloc_output_buffer, NULL); + ff_refstruct_pool_alloc_ext(sizeof(VABufferID), 0, avctx, + &vaapi_encode_alloc_output_buffer, NULL, + vaapi_encode_free_output_buffer, NULL); if (!ctx->output_buffer_pool) { err = AVERROR(ENOMEM); goto fail; @@ -2776,7 +2766,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) vaapi_encode_free(avctx, pic); } - av_buffer_pool_uninit(&ctx->output_buffer_pool); + ff_refstruct_pool_uninit(&ctx->output_buffer_pool); if (ctx->va_context != VA_INVALID_ID) { vaDestroyContext(ctx->hwctx->display, ctx->va_context); diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index bd25cd5c95..741f6c6a77 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -102,7 +102,8 @@ typedef struct VAAPIEncodePicture { int nb_param_buffers; VABufferID *param_buffers; - AVBufferRef *output_buffer_ref; + /* Refcounted via the refstruct-API */ + VABufferID *output_buffer_ref; VABufferID output_buffer; void *priv_data; @@ -262,7 +263,7 @@ typedef struct VAAPIEncodeContext { AVHWFramesContext *recon_frames; // Pool of (reusable) bitstream output buffers. - AVBufferPool *output_buffer_pool; + struct FFRefStructPool *output_buffer_pool; // Global parameters which will be applied at the start of the // sequence (includes rate control parameters below).