[FFmpeg-devel] [PATCH] lavu/hwcontext_vulkan: Support write on drm frame
Lynne
dev at lynne.ee
Wed Jun 12 03:02:07 EEST 2024
On 11/06/2024 06:55, Xiang, Haihao wrote:
> From: Haihao Xiang <haihao.xiang at intel.com>
>
> Otherwise nothing is written into the destination when a write mapping
> is requested.
>
> For example, a vulkan frame mapped from a drm frame (which is wrapped as
> a vaapi frame in the example) is used as the output of scale_vulkan
> filter, it always gets a green screen without this patch.
>
> ffmpeg -init_hw_device vaapi=va -init_hw_device vulkan=vulkan at va
> -filter_hw_device vulkan -f lavfi -i testsrc=size=352x288,format=nv12
> -vf
> "hwupload,scale_vulkan,hwmap=derive_device=vaapi:reverse=1,format=vaapi,hwdownload,format=nv12"
> -f nut - | ffplay -
>
> Signed-off-by: Haihao Xiang <haihao.xiang at intel.com>
> ---
> libavutil/hwcontext_vulkan.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
> index e2f92af227..675ead0ba6 100644
> --- a/libavutil/hwcontext_vulkan.c
> +++ b/libavutil/hwcontext_vulkan.c
> @@ -2511,7 +2511,7 @@ static inline VkFormat drm_to_vulkan_fmt(uint32_t drm_fourcc)
> }
>
> static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **frame,
> - const AVFrame *src)
> + const AVFrame *src, int flags)
> {
> int err = 0;
> VkResult ret;
> @@ -2569,6 +2569,14 @@ static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **f
> .pNext = &ext_img_mod_spec,
> .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
> };
> + int usage = (((flags & (AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE)) == (AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE)) ?
> + (VK_IMAGE_USAGE_SAMPLED_BIT |
> + VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
> + VK_IMAGE_USAGE_TRANSFER_DST_BIT |
> + VK_IMAGE_USAGE_STORAGE_BIT) :
> + ((flags & AV_HWFRAME_MAP_WRITE) ?
> + (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT) :
> + (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT)));
Changed to a more readable:
> + if (flags & AV_HWFRAME_MAP_READ)
> + create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT |
> + VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
> + if (flags & AV_HWFRAME_MAP_WRITE)
> + create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT |
> + VK_IMAGE_USAGE_TRANSFER_DST_BIT;
By filling it down below.
> VkImageCreateInfo create_info = {
> .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
> .pNext = &ext_img_spec,
> @@ -2580,8 +2588,7 @@ static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **f
> .flags = 0x0,
> .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
> .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, /* specs say so */
> - .usage = VK_IMAGE_USAGE_SAMPLED_BIT |
> - VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
> + .usage = usage,
> .samples = VK_SAMPLE_COUNT_1_BIT,
> .pQueueFamilyIndices = p->img_qfs,
> .queueFamilyIndexCount = p->nb_img_qfs,
> @@ -2788,7 +2795,7 @@ static int vulkan_map_from_drm(AVHWFramesContext *hwfc, AVFrame *dst,
> int err = 0;
> AVVkFrame *f;
>
> - if ((err = vulkan_map_from_drm_frame_desc(hwfc, &f, src)))
> + if ((err = vulkan_map_from_drm_frame_desc(hwfc, &f, src, flags)))
> return err;
>
> /* The unmapping function will free this */
Pushed, thanks.
Glad to know writing to DRM frames works, this was not tested, and it
was for a long time unsupported, due to missing modifier support.
Once Vulkan encode is added, this should be updated with ENCODE_SRC, but
hopefully I'll remember to take care of that.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0xA2FEA5F03F034464.asc
Type: application/pgp-keys
Size: 624 bytes
Desc: OpenPGP public key
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240612/4c120744/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 236 bytes
Desc: OpenPGP digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240612/4c120744/attachment.sig>
More information about the ffmpeg-devel
mailing list