From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 0E6F94E2F7 for ; Thu, 1 May 2025 05:05:49 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 31629687D2C; Thu, 1 May 2025 08:05:46 +0300 (EEST) Received: from mail-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CD2FF687BF0 for ; Thu, 1 May 2025 08:05:39 +0300 (EEST) Received: by mail-io1-f43.google.com with SMTP id ca18e2360f4ac-861d7a09c88so16655239f.2 for ; Wed, 30 Apr 2025 22:05:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746075938; x=1746680738; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZFpRzvlCXyvV0nVHdMuwSknCsHvHje2iv6/Qj2CYiH8=; b=WtYNJDqs+IGzF6rcEtolhEMgNfKSXd5Q1PLqShS78yJE6+mJG92lRPHuk0y8pvUcmn cep+bQGcXwv09XgY6f+vwIMIM5aQ7PZrzckSuZBruzXaNJb4EM4rW0KrYHN4Ed9D7sxD ciwUZLhflYmyN8UOupc5lr0zAGjw+rdfj3Txx7mhrfcv7o4SmPtvZcKqlZr/DAJG6FQe ENK0dx2vdRv0809SyWi3kRgcdZgmXBh8DlQlLgEde4UsegV2DnVEbpwPt+ZA2bVF91cg B9eEznFB32s6HW/Zy06a0VimRL3mmfRilLa3KZmDlNHwyj/H+onIry4Qe5CWjLl+Hg3T IOsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746075938; x=1746680738; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZFpRzvlCXyvV0nVHdMuwSknCsHvHje2iv6/Qj2CYiH8=; b=LR1Z3JsIUQmhWcnGXcvDsRYpqWO6JdLlj15ygJZvC1702g6W1MR5k1CLVN4naIz0I3 Kekn/9uKrfxjzsQAvrtGx9ILemQKp9KRtLkzusVMbTavDYZpk/mVKFv9t+VdvCuGGUbb JDP7FR4bNMCNjV1bJXWgfP0uIBi9nYJQYBy0mZvSi2lnS35zyRI6ctdtyvYva7pxpTGr zYfTBPjGZdJu3+7jNsvO7728l8IkLxo3v9ZNzxFMxuLK4Z8DrlV7Ni5d4bA2+9hAemVC ydp9RQYqolC6gJBKnO9JIhznmCdNJtjYchRBNr5icb6y6dRcP55v/Gigcoo3UKCauvMZ b0Zw== X-Gm-Message-State: AOJu0YyqGZtiU67iMhp/WG4w/buh6H0kszS/jTFAgSTzJXhzcERvtjDu Ydidco6qikZzrJC/cvH1DJQ/TwjqQxwA/Lh7jU3La8wejwUNRiBiERuZS0I1 X-Gm-Gg: ASbGnctukRoVze3XgFxTEEj7aAW+ydr4G9mYtHhsPNGN+bojq1Ikmww1U9W4eRjxznF Ke9SiSyLPXEwukkbUNJ0WNjQbpYVRApbnPJMYLUzkXfwk8Em+diLp2MLXEugh1fX4cKvQs58Qwg EeyeKnGac7xSSx2d5IlTerHprKMO5qD6P0FiekZ15mRDU70gyXod0Y1etphauLvRIEiNIZkt+Zm a1LpeyydGwUX58p+KX2O6GFLvpJpYdgmSofVqMIDY3b/YESNBz9WJQfZm0y17Vev/+bGBCtivQw 0zUr4meJvZsBg+xu10eNVCIxgPzQ8g7UOUj+qdC7AKs/rR2bmJmPspXIpRNm+fQ37uRwUKUicWi O/t6ONzEnm5s3wGqeS8OIVAY/zMtcZa+GR7aNAZu+ X-Google-Smtp-Source: AGHT+IFRTrMkq+KizOejQnoHLuDZkWxGdIoT67qRp9Xb42yJPbchUQNqGfiFO9O6HS9NQ4pKNgeHVA== X-Received: by 2002:a05:6e02:18c5:b0:3d9:6e2d:c7dd with SMTP id e9e14a558f8ab-3d96f22d8a8mr20002165ab.17.1746075938028; Wed, 30 Apr 2025 22:05:38 -0700 (PDT) Received: from russell-shotover-arch.lan (c-71-196-175-158.hsd1.co.comcast.net. [71.196.175.158]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4f863119001sm1089771173.91.2025.04.30.22.05.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Apr 2025 22:05:37 -0700 (PDT) From: Russell Greene To: ffmpeg-devel@ffmpeg.org Date: Wed, 30 Apr 2025 23:05:03 -0600 Message-ID: <20250501050527.582070-1-russellgreene8@gmail.com> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] hwcontext_vulkan: fix exporting multi-plane DRM modifiers 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: Russell Greene , Russell Greene Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Russell Greene Previously, it was assumed that `drmFormatModifierPlaneCount` was one for all modifiers when exporting, which is not always the case, in particular for AMD GPUs and maybe others. Fetch the number of memory planes and fill the structs appropriately in this situation. The encoded stream is still bad in the case whre modifers are involved, but I think this patch still stands on its own and I suspect that may be a driver bug. A potential improvement that could be make is to cache the format information, so we can avoid the two GetPhysicalDeviceFormatProperties2 calls for each export, as well as the allocation. I doubt this is very expensive, but seemed worth noting. Signed-off-by: Russell Greene --- libavutil/hwcontext_vulkan.c | 76 +++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c index ade0235ef1..d14fa4655b 100644 --- a/libavutil/hwcontext_vulkan.c +++ b/libavutil/hwcontext_vulkan.c @@ -3787,6 +3787,17 @@ static inline uint32_t vulkan_fmt_to_drm(VkFormat vkfmt) return DRM_FORMAT_INVALID; } +#define MAX_MEMORY_PLANES 4 +static VkImageAspectFlags plane_index_to_aspect(int plane) { + if (plane == 0) return VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT; + if (plane == 1) return VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT; + if (plane == 2) return VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT; + if (plane == 3) return VK_IMAGE_ASPECT_MEMORY_PLANE_3_BIT_EXT; + + av_assert2 (false && "Invalid plane index"); + return VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT; +} + static int vulkan_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags) { @@ -3855,14 +3866,65 @@ static int vulkan_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst, drm_desc->nb_layers = planes; for (int i = 0; i < drm_desc->nb_layers; i++) { - VkSubresourceLayout layout; - VkImageSubresource sub = { - .aspectMask = VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT, + VkDrmFormatModifierPropertiesListEXT modp = { + .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT, + }; + VkFormatProperties2 fmtp = { + .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, + .pNext = &modp, }; VkFormat plane_vkfmt = av_vkfmt_from_pixfmt(hwfc->sw_format)[i]; - drm_desc->layers[i].format = vulkan_fmt_to_drm(plane_vkfmt); - drm_desc->layers[i].nb_planes = 1; + drm_desc->layers[i].format = vulkan_fmt_to_drm(plane_vkfmt); + + /* query drmFormatModifierCount by keeping pDrmFormatModifierProperties NULL */ + vk->GetPhysicalDeviceFormatProperties2(hwctx->phys_dev, plane_vkfmt, &fmtp); + + modp.pDrmFormatModifierProperties = + av_calloc(modp.drmFormatModifierCount, sizeof(*modp.pDrmFormatModifierProperties)); + if (!modp.pDrmFormatModifierProperties) { + err = AVERROR(ENOMEM); + goto end; + } + vk->GetPhysicalDeviceFormatProperties2(hwctx->phys_dev, plane_vkfmt, &fmtp); + + VkDrmFormatModifierPropertiesEXT *mod_props = NULL; + for (uint32_t i = 0; i < modp.drmFormatModifierCount; ++i) { + VkDrmFormatModifierPropertiesEXT *m = &modp.pDrmFormatModifierProperties[i]; + if (m->drmFormatModifier == drm_mod.drmFormatModifier) { + mod_props = m; + break; + } + } + + if (!mod_props) { + av_free(modp.pDrmFormatModifierProperties); + av_log(hwfc, AV_LOG_ERROR, "Cannot fetch modifier properties for modifier "PRIu64"!\n", + drm_mod.drmFormatModifier); + err = AVERROR_EXTERNAL; + goto end; + } + drm_desc->layers[i].nb_planes = mod_props->drmFormatModifierPlaneCount; + av_free(modp.pDrmFormatModifierProperties); + + if (drm_desc->layers[i].nb_planes > MAX_MEMORY_PLANES) { + av_log(hwfc, AV_LOG_ERROR, "Too many memory planes for DRM format!\n"); + err = AVERROR_EXTERNAL; + goto end; + } + + for (int j = 0; j < drm_desc->layers[i].nb_planes; j++) { + VkSubresourceLayout layout; + VkImageSubresource sub = { + .aspectMask = plane_index_to_aspect(j), + }; + + drm_desc->layers[i].planes[j].object_index = FFMIN(i, drm_desc->nb_objects - 1); + + vk->GetImageSubresourceLayout(hwctx->act_dev, f->img[i], &sub, &layout); + drm_desc->layers[i].planes[j].offset = layout.offset; + drm_desc->layers[i].planes[j].pitch = layout.rowPitch; + } if (drm_desc->layers[i].format == DRM_FORMAT_INVALID) { av_log(hwfc, AV_LOG_ERROR, "Cannot map to DRM layer, unsupported!\n"); @@ -3870,14 +3932,10 @@ static int vulkan_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst, goto end; } - drm_desc->layers[i].planes[0].object_index = FFMIN(i, drm_desc->nb_objects - 1); if (f->tiling == VK_IMAGE_TILING_OPTIMAL) continue; - vk->GetImageSubresourceLayout(hwctx->act_dev, f->img[i], &sub, &layout); - drm_desc->layers[i].planes[0].offset = layout.offset; - drm_desc->layers[i].planes[0].pitch = layout.rowPitch; } dst->width = src->width; -- 2.49.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".