From 603e7c3e5fa2c65cc5db241a2533426d59e588ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Manuel=20J=C3=A1quez=20Leal?= Date: Thu, 3 Oct 2024 21:04:28 +0200 Subject: [PATCH] vulkan: add gst_vulkan_video_image_create_view() This function is moved from gstvkdecoder-private so it could be used by gstvkencoder-private too, removing there what it should be duplicated code. Part-of: --- .../gst/vulkan/gstvkdecoder-private.c | 51 +------------ .../gst/vulkan/gstvkencoder-private.c | 49 ++----------- .../gst-libs/gst/vulkan/gstvkvideo-private.c | 71 +++++++++++++++++++ .../gst-libs/gst/vulkan/gstvkvideo-private.h | 5 ++ 4 files changed, 82 insertions(+), 94 deletions(-) diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkdecoder-private.c b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkdecoder-private.c index fb42ff83ff..db859a5ffc 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkdecoder-private.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkdecoder-private.c @@ -1088,61 +1088,14 @@ gst_vulkan_decoder_picture_create_view (GstVulkanDecoder * self, GstBuffer * buf, gboolean is_out) { GstVulkanDecoderPrivate *priv; - VkSamplerYcbcrConversionInfo yuv_sampler_info; - VkImageViewCreateInfo view_create_info; - GstVulkanImageMemory *vkmem; - GstMemory *mem; - gpointer pnext; - guint n_mems; g_return_val_if_fail (GST_IS_VULKAN_DECODER (self) && GST_IS_BUFFER (buf), NULL); - n_mems = gst_buffer_n_memory (buf); - if (n_mems != 1) - return NULL; - - mem = gst_buffer_peek_memory (buf, 0); - if (!gst_is_vulkan_image_memory (mem)) - return NULL; - priv = gst_vulkan_decoder_get_instance_private (self); - pnext = NULL; - if (priv->sampler) { - /* *INDENT-OFF* */ - yuv_sampler_info = (VkSamplerYcbcrConversionInfo) { - .sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO, - .conversion = priv->sampler->handle, - }; - /* *INDENT-ON* */ - - pnext = &yuv_sampler_info; - } - - vkmem = (GstVulkanImageMemory *) mem; - - /* *INDENT-OFF* */ - view_create_info = (VkImageViewCreateInfo) { - .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .pNext = pnext, - .viewType = self->layered_dpb && !is_out ? - VK_IMAGE_VIEW_TYPE_2D_ARRAY: VK_IMAGE_VIEW_TYPE_2D, - .format = vkmem->create_info.format, - .image = vkmem->image, - .components = _vk_identity_component_map, - .subresourceRange = (VkImageSubresourceRange) { - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .baseArrayLayer = 0, - .layerCount = self->layered_dpb && !is_out ? - VK_REMAINING_ARRAY_LAYERS : 1, - .levelCount = 1, - }, - }; - /* *INDENT-ON* */ - - return gst_vulkan_get_or_create_image_view_with_info (vkmem, - &view_create_info); + return gst_vulkan_video_image_create_view (buf, self->layered_dpb, is_out, + priv->sampler); } /** diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkencoder-private.c b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkencoder-private.c index ed10737a1c..f5cbbb42e5 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkencoder-private.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkencoder-private.c @@ -372,45 +372,6 @@ gst_vulkan_encoder_new_video_session_parameters (GstVulkanEncoder * self, priv->vk.DestroyVideoSessionParameters); } -static GstVulkanImageView * -gst_vulkan_encoder_get_image_view_from_buffer (GstVulkanEncoder * self, - GstBuffer * buf, gboolean dpb) -{ - GstVulkanEncoderPrivate *priv = - gst_vulkan_encoder_get_instance_private (self); - VkImageViewCreateInfo view_create_info; - GstVulkanImageMemory *vkmem; - GstMemory *mem; - guint n_mems; - - n_mems = gst_buffer_n_memory (buf); - g_assert (n_mems == 1); - - mem = gst_buffer_peek_memory (buf, 0); - g_assert (gst_is_vulkan_image_memory (mem)); - - vkmem = (GstVulkanImageMemory *) mem; - - /* *INDENT-OFF* */ - view_create_info = (VkImageViewCreateInfo) { - .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .viewType = (dpb && priv->layered_dpb) ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D, - .format = vkmem->create_info.format, - .image = vkmem->image, - .components = _vk_identity_component_map, - .subresourceRange = (VkImageSubresourceRange) { - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .baseArrayLayer = (dpb && priv->layered_dpb) ? priv->current_slot_index : 0, - .layerCount = 1, - .levelCount = 1, - }, - }; - /* *INDENT-ON* */ - - return gst_vulkan_get_or_create_image_view_with_info (vkmem, - &view_create_info); -} - /** * gst_vulkan_encode_picture_new: * @self: the #GstVulkanEncoder with the pool's configuration. @@ -1141,9 +1102,8 @@ gst_vulkan_encoder_encode (GstVulkanEncoder * self, /* Set the ref slots according to the pic refs to bound the video session encoding. It should contain all the references + 1 to book a new slotIndex (-1) for the current picture. */ - pic->dpb_view = - gst_vulkan_encoder_get_image_view_from_buffer (self, pic->dpb_buffer, - TRUE); + pic->dpb_view = gst_vulkan_video_image_create_view (pic->dpb_buffer, + priv->layered_dpb, FALSE, NULL); /* *INDENT-OFF* */ pic->dpb = (VkVideoPictureResourceInfoKHR) { @@ -1248,9 +1208,8 @@ gst_vulkan_encoder_encode (GstVulkanEncoder * self, /* Peek the output memory to be used by VkVideoEncodeInfoKHR.dstBuffer */ mem = gst_buffer_peek_memory (pic->out_buffer, n_mems); /* Peek the image view to be encoded */ - pic->img_view = - gst_vulkan_encoder_get_image_view_from_buffer (self, pic->in_buffer, - FALSE); + pic->img_view = gst_vulkan_video_image_create_view (pic->in_buffer, + priv->layered_dpb, TRUE, NULL); /* Attribute a free slot index to the picture to be used later as a reference. * The picture is kept until it remains useful to the encoding process.*/ diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.c b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.c index 1643e8a7fd..64a79bc958 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.c @@ -241,3 +241,74 @@ gst_vulkan_video_codec_buffer_new (GstVulkanDevice * device, gst_buffer_append_memory (buf, mem); return buf; } + +/** + * gst_vulkan_video_image_create_view: + * @buf: a #GstBuffer + * @layered_dpb: if DPB is layered + * @is_out: if @buf is for output or for DPB + * @sampler: (optional): sampler #GstVulkanHandle + * + * Creates a #GstVulkanImageView for @buf for decoding, with the internal Ycbcr + * sampler, if available. + * + * Returns: (transfer full) (nullable): the #GstVulkanImageView. + */ +GstVulkanImageView * +gst_vulkan_video_image_create_view (GstBuffer * buf, gboolean layered_dpb, + gboolean is_out, GstVulkanHandle * sampler) +{ + VkSamplerYcbcrConversionInfo yuv_sampler_info; + VkImageViewCreateInfo view_create_info; + GstVulkanImageMemory *vkmem; + GstMemory *mem; + gpointer pnext; + guint n_mems; + + g_return_val_if_fail (GST_IS_BUFFER (buf), NULL); + + n_mems = gst_buffer_n_memory (buf); + if (n_mems != 1) + return NULL; + + mem = gst_buffer_peek_memory (buf, 0); + if (!gst_is_vulkan_image_memory (mem)) + return NULL; + + pnext = NULL; + if (sampler + && sampler->type == GST_VULKAN_HANDLE_TYPE_SAMPLER_YCBCR_CONVERSION) { + yuv_sampler_info = (VkSamplerYcbcrConversionInfo) { + /* *INDENT-OFF* */ + .sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO, + .conversion = sampler->handle, + /* *INDENT-ON* */ + }; + + pnext = &yuv_sampler_info; + } + + vkmem = (GstVulkanImageMemory *) mem; + + /* *INDENT-OFF* */ + view_create_info = (VkImageViewCreateInfo) { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .pNext = pnext, + .viewType = layered_dpb && !is_out ? + VK_IMAGE_VIEW_TYPE_2D_ARRAY: VK_IMAGE_VIEW_TYPE_2D, + .format = vkmem->create_info.format, + .image = vkmem->image, + .components = _vk_identity_component_map, + .subresourceRange = (VkImageSubresourceRange) { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseArrayLayer = 0, + .layerCount = layered_dpb && !is_out ? + VK_REMAINING_ARRAY_LAYERS : 1, + .levelCount = 1, + }, + }; + /* *INDENT-ON* */ + + return gst_vulkan_get_or_create_image_view_with_info (vkmem, + &view_create_info); +} diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.h b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.h index 638d407008..2c27ecaad3 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/vulkan/gstvkvideo-private.h @@ -91,4 +91,9 @@ GstBuffer * gst_vulkan_video_codec_buffer_new (GstVulkanDevice VkBufferUsageFlags usage, gsize size); +GstVulkanImageView * gst_vulkan_video_image_create_view (GstBuffer * buf, + gboolean layered_dpb, + gboolean is_out, + GstVulkanHandle * sampler); + G_END_DECLS