From patchwork Sat May 13 15:42:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 41602 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba91:b0:105:feb:71f2 with SMTP id fb17csp450230pzb; Sat, 13 May 2023 08:42:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4QZiQ0w2awSAVR5Ri6cS6JoxIgagwNzaBVQLEgPjlYkt2xvmQuCiDzsGTlRkEmGwhrDNnr X-Received: by 2002:a17:907:8694:b0:966:2aab:ae51 with SMTP id qa20-20020a170907869400b009662aabae51mr20322602ejc.11.1683992567835; Sat, 13 May 2023 08:42:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683992567; cv=none; d=google.com; s=arc-20160816; b=JuJmexhTXD6X5AL+baQbCNh9WemFem+hgCHBcI3dzx3LbJBAoacHCFYKDTelNr+P7z v77ciHcvuRpXj4Il1rgCaYGC8t2m7BCHNNN2Tlqo//VIszMBhBN5fUMQr5N8VHVD4sI8 PCmEMzwZRoyQE2NJfPu/4EPVScIeSxaYLtxgCvX0coYFj/bmv8y4D5UBBHLT6/afVWre pVqlDzA+XyaQKKzREiiRHSmIcajN56Gz66xoxkK0fO+WSMSdI0YmxLdpyKWdalUwX1nb S8j3jLCYWhOdYsPIbv8AYCTVoIZoGOwAQ+hcLSO5L6Sn6BsAH4OgaYj5KInmBB2Bdn8V Xcwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=biHEZuiHvN7ST4WrXLqIK9Ns2KvWZJLug91BQRASDhQ=; b=OePl6h2jeIdlYrY39dTWTj6a0MBsr07W6qGUlC9oyyrj93f4fxDoCzf486CFYn5S74 r49XmBJh3VA2Ko80dN893NAHzKIXKzOrt2Lf6UpiIlrD3Gk9VGAQZBKUqJiL2in8ALzh 0Xxv3d5F4wKrS7nrYyAQksBRFPzAnYsyj1daQgFoFLYbSg6LS0dugvwM67T79G8PSMCz PXPMpUHuUDsXgmSNzcCcKvdAIQLqnYyv5SxQqZu08TG+cXyjyUkoPQNldeSwj278HFrW 0wyIzOv2b9P6z+oUs/maO5SI7J1Z8c6/JfKWwj/faVrmnFn6ulevwT057wnZhcTfcM/9 aRiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=lWq+mTQK; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id vu8-20020a170907a64800b00969f3a182a3si9687318ejc.519.2023.05.13.08.42.47; Sat, 13 May 2023 08:42:47 -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=@haasn.xyz header.s=mail header.b=lWq+mTQK; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D09DB68BF96; Sat, 13 May 2023 18:42:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 78A1568BF34 for ; Sat, 13 May 2023 18:42:35 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id E78DB47269; Sat, 13 May 2023 17:42:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1683992554; bh=vjnLs3XGsiXmkWH1nN7ZDUEk7OFLSlrO+309Yy5ED8w=; h=From:To:Cc:Subject:Date:From; b=lWq+mTQKw6P+uPQDgAV6cV4q3R1EhsdOklun31bgq2zNcsBYYqt81w/EvLNBRB49D N9Scej5fnVWq/pjsNhZ/a8FoXuYN58aOehU8NZ+I3VPB7r504QurxMbJqyMv8BKI1D rb+fQbwMqR7dP0rxBo0ti0uQY84k6yqrHcf5K074= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sat, 13 May 2023 17:42:30 +0200 Message-Id: <20230513154230.27485-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3] lavfi/vf_libplacebo: allow operation without avhwdevice 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: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: wBpKMh2FwoiE From: Niklas Haas Recent versions of libplacebo have required Vulkan versions incompatible with lavu Vulkan hwcontexts. While this is expected to change eventually, breaking vf_libplacebo every time there is such a transition period is obviously undesired behavior, as the following sea of bug reports shows. This commit adds a fallback path for init_vulkan failures which simply creates an internal device if there was no user-supplied Vulkan hwaccel. Useful when no interop with lavu vulkan hwframes is needed or desired, and makes using this filter easier inside certain applications. Fixes: https://github.com/haasn/libplacebo/issues/170 Fixes: https://github.com/mpv-player/mpv/issues/9589#issuecomment-1535432185 Fixes: https://github.com/mpv-player/mpv/issues/11363 Fixes: https://github.com/mpv-player/mpv/issues/11685#issuecomment-1546627082 Closes: https://code.videolan.org/videolan/libplacebo/-/issues/270 --- libavfilter/vf_libplacebo.c | 87 +++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c index 6fe3e0ea882..8387f4dd47f 100644 --- a/libavfilter/vf_libplacebo.c +++ b/libavfilter/vf_libplacebo.c @@ -351,56 +351,47 @@ fail: return err; } -static int init_vulkan(AVFilterContext *avctx) +static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwctx) { int err = 0; LibplaceboContext *s = avctx->priv; - const AVHWDeviceContext *avhwctx; - const AVVulkanDeviceContext *hwctx; uint8_t *buf = NULL; size_t buf_len; - if (!avctx->hw_device_ctx) { - av_log(s, AV_LOG_ERROR, "Missing vulkan hwdevice for vf_libplacebo.\n"); - return AVERROR(EINVAL); - } - - avhwctx = (AVHWDeviceContext *) avctx->hw_device_ctx->data; - if (avhwctx->type != AV_HWDEVICE_TYPE_VULKAN) { - av_log(s, AV_LOG_ERROR, "Expected vulkan hwdevice for vf_libplacebo, got %s.\n", - av_hwdevice_get_type_name(avhwctx->type)); - return AVERROR(EINVAL); + if (hwctx) { + /* Import libavfilter vulkan context into libplacebo */ + s->vulkan = pl_vulkan_import(s->log, pl_vulkan_import_params( + .instance = hwctx->inst, + .get_proc_addr = hwctx->get_proc_addr, + .phys_device = hwctx->phys_dev, + .device = hwctx->act_dev, + .extensions = hwctx->enabled_dev_extensions, + .num_extensions = hwctx->nb_enabled_dev_extensions, + .features = &hwctx->device_features, + .queue_graphics = { + .index = hwctx->queue_family_index, + .count = hwctx->nb_graphics_queues, + }, + .queue_compute = { + .index = hwctx->queue_family_comp_index, + .count = hwctx->nb_comp_queues, + }, + .queue_transfer = { + .index = hwctx->queue_family_tx_index, + .count = hwctx->nb_tx_queues, + }, + /* This is the highest version created by hwcontext_vulkan.c */ + .max_api_version = VK_API_VERSION_1_2, + )); + } else { + s->vulkan = pl_vulkan_create(s->log, pl_vulkan_params( + .queue_count = 0, /* enable all queues for parallelization */ + )); } - hwctx = avhwctx->hwctx; - - /* Import libavfilter vulkan context into libplacebo */ - s->vulkan = pl_vulkan_import(s->log, pl_vulkan_import_params( - .instance = hwctx->inst, - .get_proc_addr = hwctx->get_proc_addr, - .phys_device = hwctx->phys_dev, - .device = hwctx->act_dev, - .extensions = hwctx->enabled_dev_extensions, - .num_extensions = hwctx->nb_enabled_dev_extensions, - .features = &hwctx->device_features, - .queue_graphics = { - .index = hwctx->queue_family_index, - .count = hwctx->nb_graphics_queues, - }, - .queue_compute = { - .index = hwctx->queue_family_comp_index, - .count = hwctx->nb_comp_queues, - }, - .queue_transfer = { - .index = hwctx->queue_family_tx_index, - .count = hwctx->nb_tx_queues, - }, - /* This is the highest version created by hwcontext_vulkan.c */ - .max_api_version = VK_API_VERSION_1_2, - )); - if (!s->vulkan) { - av_log(s, AV_LOG_ERROR, "Failed importing vulkan device to libplacebo!\n"); + av_log(s, AV_LOG_ERROR, "Failed %s Vulkan device!\n", + hwctx ? "importing" : "creating"); err = AVERROR_EXTERNAL; goto fail; } @@ -695,10 +686,17 @@ static int libplacebo_query_format(AVFilterContext *ctx) { int err; LibplaceboContext *s = ctx->priv; + const AVVulkanDeviceContext *vkhwctx = NULL; const AVPixFmtDescriptor *desc = NULL; AVFilterFormats *infmts = NULL, *outfmts = NULL; - RET(init_vulkan(ctx)); + if (ctx->hw_device_ctx) { + const AVHWDeviceContext *avhwctx = (void *) ctx->hw_device_ctx->data; + if (avhwctx->type == AV_HWDEVICE_TYPE_VULKAN) + vkhwctx = avhwctx->hwctx; + } + + RET(init_vulkan(ctx, vkhwctx)); while ((desc = av_pix_fmt_desc_next(desc))) { enum AVPixelFormat pixfmt = av_pix_fmt_desc_get_id(desc); @@ -710,6 +708,11 @@ static int libplacebo_query_format(AVFilterContext *ctx) continue; #endif + if (pixfmt == AV_PIX_FMT_VULKAN) { + if (!vkhwctx || vkhwctx->act_dev != s->vulkan->device) + continue; + } + if (!pl_test_pixfmt(s->gpu, pixfmt)) continue;