Skip to content

Truly independent web browser

LibMedia: Copy Vulkan video frames that occupy the DPB on old FFmpeg

Zaggy10245b3518048159
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h
index 4354d95f29f..192f14063c8 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h
@@ -71,7 +71,9 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(A
F(av_buffer_unref) \
F(av_hwdevice_ctx_alloc) \
F(av_hwdevice_ctx_init) \
+ F(av_hwframe_ctx_alloc) \
F(av_hwframe_ctx_init) \
+ F(av_hwframe_get_buffer) \
F(av_hwframe_map) \
F(av_malloc) \
F(av_opt_set_int) \
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
index 37f986f956a..3cad186253e 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
@@ -455,6 +455,12 @@ FFmpegVulkanVideoDecoder::~FFmpegVulkanVideoDecoder()
free_decoding_frames(decoding_frames);
m_outputs.clear();
+ // A stalled copy may still be pending on its command buffer.
+ if (m_copy_command_pool != VK_NULL_HANDLE && !m_copy_stalled)
+ vkDestroyCommandPool(m_device.logical_device(), m_copy_command_pool, nullptr);
+ m_functions.av_buffer_unref(&m_exportable_frames_context);
+ m_functions.av_buffer_unref(&m_frames_context_copied_from);
+
m_functions.av_packet_free(&m_packet);
m_functions.av_frame_free(&m_received_frame);
m_functions.avcodec_free_context(&m_codec_context);
@@ -499,21 +505,29 @@ DecoderErrorOr<void> FFmpegVulkanVideoDecoder::create_frames_context()
// Storage use is only for FFmpeg's own filters, and can rule out the modifiers that export.
vulkan_frames_context.usage = static_cast<VkImageUsageFlagBits>(vulkan_frames_context.usage & ~VK_IMAGE_USAGE_STORAGE_BIT);
- m_drm_format_modifiers = m_device.exportable_drm_format_modifiers(vulkan_frames_context.format[0], vulkan_frames_context.usage, vulkan_frames_context.img_flags, vulkan_frames_context.create_pnext);
- if (m_drm_format_modifiers.is_empty())
- return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "No DRM format modifier can export the decoded images"sv);
-
- // FFmpeg chains the video profile in, and leaves choosing a modifier to us.
- m_drm_format_modifier_list = {};
- m_drm_format_modifier_list.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT;
- m_drm_format_modifier_list.pNext = vulkan_frames_context.create_pnext;
- m_drm_format_modifier_list.drmFormatModifierCount = m_drm_format_modifiers.size();
- m_drm_format_modifier_list.pDrmFormatModifiers = m_drm_format_modifiers.data();
- vulkan_frames_context.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
- vulkan_frames_context.create_pnext = &m_drm_format_modifier_list;
+ // FIXME: Decide this through the feature blocklist. Until 63.20.101, FFmpeg gives a separate DPB the output's
+ // modifiers, which RADV lays out wrongly. Frames used as references can't be copied, since a later decode
+ // may be using them.
+ auto output_is_used_as_reference = (vulkan_frames_context.usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR) != 0;
+ auto copies_decoded_frames = &m_functions != &FFmpegFunctions::bundled() && m_functions.avcodec_version() < AV_VERSION_INT(63, 20, 101) && !output_is_used_as_reference;
+
+ if (!copies_decoded_frames) {
+ m_drm_format_modifiers = m_device.exportable_drm_format_modifiers(vulkan_frames_context.format[0], vulkan_frames_context.usage, vulkan_frames_context.img_flags, vulkan_frames_context.create_pnext);
+ if (m_drm_format_modifiers.is_empty())
+ return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "No DRM format modifier can export the decoded images"sv);
+
+ // FFmpeg chains the video profile in, and leaves choosing a modifier to us.
+ m_drm_format_modifier_list = {};
+ m_drm_format_modifier_list.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT;
+ m_drm_format_modifier_list.pNext = vulkan_frames_context.create_pnext;
+ m_drm_format_modifier_list.drmFormatModifierCount = m_drm_format_modifiers.size();
+ m_drm_format_modifier_list.pDrmFormatModifiers = m_drm_format_modifiers.data();
+ vulkan_frames_context.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
+ vulkan_frames_context.create_pnext = &m_drm_format_modifier_list;
+ }
if (m_functions.av_hwframe_ctx_init(frames_reference) < 0)
- return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "FFmpeg could not create exportable Vulkan frames"sv);
+ return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "FFmpeg could not create Vulkan frames"sv);
m_functions.av_buffer_unref(&m_codec_context->hw_frames_ctx);
m_codec_context->hw_frames_ctx = frames_reference;
@@ -650,12 +664,18 @@ void FFmpegVulkanVideoDecoder::complete_frames_until_exit()
DecoderErrorOr<FFmpegVulkanVideoDecoder::DecodedOutput> FFmpegVulkanVideoDecoder::complete_frame(DecodingFrame& decoding_frame)
{
ScopeGuard free_frame = [&] { m_functions.av_frame_free(&decoding_frame.frame); };
- auto& frame = *decoding_frame.frame;
auto logical_device = m_device.logical_device();
if (!wait_for_semaphore(logical_device, decoding_frame.semaphore, decoding_frame.decoded_semaphore_value))
return DecoderError::with_description(DecoderErrorCategory::Unknown, "The Vulkan device failed or stalled while decoding a frame"sv);
+ if (static_cast<AVVulkanFramesContext const*>(frames_context_of(*decoding_frame.frame).hwctx)->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
+ auto* copied_frame = TRY(copy_into_exportable_frame(*decoding_frame.frame, decoding_frame.semaphore, decoding_frame.decoded_semaphore_value));
+ m_functions.av_frame_free(&decoding_frame.frame);
+ decoding_frame.frame = copied_frame;
+ }
+ auto& frame = *decoding_frame.frame;
+
// Mapping the frame to dma-bufs hands its image over to whichever device samples it next, which the frame then waits
// for, so that it leaves the decoder ready to be drawn.
auto* exported_frame = m_functions.av_frame_alloc();
@@ -680,6 +700,229 @@ DecoderErrorOr<FFmpegVulkanVideoDecoder::DecodedOutput> FFmpegVulkanVideoDecoder
return DecodedOutput { move(surface), move(contents), decoding_frame.timing, size, bit_depth };
}
+DecoderErrorOr<void> FFmpegVulkanVideoDecoder::prepare_to_copy_frames_from(AVFrame const& frame)
+{
+ if (m_frames_context_copied_from != nullptr && m_frames_context_copied_from->data == frame.hw_frames_ctx->data)
+ return {};
+
+ auto* logical_device = m_device.logical_device();
+ if (m_copy_command_pool == VK_NULL_HANDLE) {
+ VkCommandPoolCreateInfo pool_info {};
+ pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+ pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
+ pool_info.queueFamilyIndex = m_device.compute_queue_family();
+ if (vkCreateCommandPool(logical_device, &pool_info, nullptr, &m_copy_command_pool) != VK_SUCCESS)
+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Could not create a command pool for copying decoded frames"sv);
+ }
+ if (m_copy_command_buffer == VK_NULL_HANDLE) {
+ VkCommandBufferAllocateInfo buffer_info {};
+ buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+ buffer_info.commandPool = m_copy_command_pool;
+ buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
+ buffer_info.commandBufferCount = 1;
+ if (vkAllocateCommandBuffers(logical_device, &buffer_info, &m_copy_command_buffer) != VK_SUCCESS)
+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Could not allocate a command buffer for copying decoded frames"sv);
+ }
+
+ m_functions.av_buffer_unref(&m_exportable_frames_context);
+ m_functions.av_buffer_unref(&m_frames_context_copied_from);
+
+ auto const& source_context = frames_context_of(frame);
+ auto const& source_vulkan_context = *static_cast<AVVulkanFramesContext const*>(source_context.hwctx);
+ if (source_vulkan_context.format[1] != VK_FORMAT_UNDEFINED)
+ return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "Decoded frames that span several images cannot be copied"sv);
+
+ VkImageUsageFlags const usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
+ m_exportable_drm_format_modifiers = m_device.exportable_drm_format_modifiers(source_vulkan_context.format[0], usage, 0, source_vulkan_context.create_pnext);
+ if (m_exportable_drm_format_modifiers.is_empty())
+ return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "No DRM format modifier can export copies of the decoded images"sv);
+
+ auto* reference = m_functions.av_hwframe_ctx_alloc(m_codec_context->hw_device_ctx);
+ if (reference == nullptr)
+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Could not allocate a frames context for exportable frames"sv);
+ ArmedScopeGuard free_reference = [&] { m_functions.av_buffer_unref(&reference); };
+
+ auto& context = *reinterpret_cast<AVHWFramesContext*>(reference->data);
+ context.format = AV_PIX_FMT_VULKAN;
+ context.sw_format = source_context.sw_format;
+ context.width = source_context.width;
+ context.height = source_context.height;
+
+ // FFmpeg only leaves the usage and flags as given for modifier-tiled images when a video profile list is chained.
+ m_exportable_drm_format_modifier_list = {};
+ m_exportable_drm_format_modifier_list.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT;
+ m_exportable_drm_format_modifier_list.pNext = source_vulkan_context.create_pnext;
+ m_exportable_drm_format_modifier_list.drmFormatModifierCount = m_exportable_drm_format_modifiers.size();
+ m_exportable_drm_format_modifier_list.pDrmFormatModifiers = m_exportable_drm_format_modifiers.data();
+
+ auto& vulkan_context = *static_cast<AVVulkanFramesContext*>(context.hwctx);
+ vulkan_context.format[0] = source_vulkan_context.format[0];
+ vulkan_context.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
+ vulkan_context.usage = static_cast<VkImageUsageFlagBits>(usage);
+ vulkan_context.create_pnext = &m_exportable_drm_format_modifier_list;
+
+ if (m_functions.av_hwframe_ctx_init(reference) < 0)
+ return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "FFmpeg could not create exportable Vulkan frames"sv);
+
+ m_frames_context_copied_from = m_functions.av_buffer_ref(frame.hw_frames_ctx);
+ if (m_frames_context_copied_from == nullptr)
+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Could not reference the decoded frames' context"sv);
+
+ free_reference.disarm();
+ m_exportable_frames_context = reference;
+ return {};
+}
+
+DecoderErrorOr<AVFrame*> FFmpegVulkanVideoDecoder::copy_into_exportable_frame(AVFrame const& frame, VkSemaphore decoded_semaphore, u64 decoded_semaphore_value)
+{
+ if (m_copy_stalled)
+ return DecoderError::with_description(DecoderErrorCategory::Unknown, "The Vulkan device stalled while copying an earlier frame"sv);
+ TRY(prepare_to_copy_frames_from(frame));
+
+ auto* copy = m_functions.av_frame_alloc();
+ if (copy == nullptr)
+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to allocate FFmpeg frame"sv);
+ ArmedScopeGuard free_copy = [&] { m_functions.av_frame_free(&copy); };
+ if (m_functions.av_hwframe_get_buffer(m_exportable_frames_context, copy, 0) < 0)
+ return DecoderError::with_description(DecoderErrorCategory::Memory, "Could not allocate an exportable Vulkan frame"sv);
+ copy->width = frame.width;
+ copy->height = frame.height;
+
+ auto& source_context = frames_context_of(frame);
+ auto& source_vulkan_context = *static_cast<AVVulkanFramesContext*>(source_context.hwctx);
+ auto* source = reinterpret_cast<AVVkFrame*>(frame.data[0]);
+ auto& destination_context = frames_context_of(*copy);
+ auto& destination_vulkan_context = *static_cast<AVVulkanFramesContext*>(destination_context.hwctx);
+ auto* destination = reinterpret_cast<AVVkFrame*>(copy->data[0]);
+
+ VkCommandBufferBeginInfo begin_info {};
+ begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+ begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
+ if (vkBeginCommandBuffer(m_copy_command_buffer, &begin_info) != VK_SUCCESS)
+ return DecoderError::with_description(DecoderErrorCategory::Unknown, "Could not begin recording a frame copy"sv);
+
+ VkImageSubresourceRange const whole_image { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
+
+ source_vulkan_context.lock_frame(&source_context, source);
+ VkImageMemoryBarrier2 prepare_barriers[2] {};
+ prepare_barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
+ prepare_barriers[0].srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
+ prepare_barriers[0].dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT;
+ prepare_barriers[0].dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT;
+ prepare_barriers[0].oldLayout = source->layout[0];
+ prepare_barriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
+ prepare_barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ prepare_barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ prepare_barriers[0].image = source->img[0];
+ prepare_barriers[0].subresourceRange = whole_image;
+ source->layout[0] = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
+ source->access[0] = VK_ACCESS_2_TRANSFER_READ_BIT;
+ source_vulkan_context.unlock_frame(&source_context, source);
+
+ // The image's previous contents are discarded, so whichever device last read it needs no handing back.
+ prepare_barriers[1].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
+ prepare_barriers[1].srcStageMask = VK_PIPELINE_STAGE_2_NONE;
+ prepare_barriers[1].dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT;
+ prepare_barriers[1].dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
+ prepare_barriers[1].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+ prepare_barriers[1].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
+ prepare_barriers[1].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ prepare_barriers[1].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ prepare_barriers[1].image = destination->img[0];
+ prepare_barriers[1].subresourceRange = whole_image;
+
+ VkDependencyInfo prepare_dependency {};
+ prepare_dependency.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
+ prepare_dependency.imageMemoryBarrierCount = 2;
+ prepare_dependency.pImageMemoryBarriers = prepare_barriers;
+ vkCmdPipelineBarrier2(m_copy_command_buffer, &prepare_dependency);
+
+ VkImageCopy regions[2] {};
+ regions[0].srcSubresource = { VK_IMAGE_ASPECT_PLANE_0_BIT, 0, 0, 1 };
+ regions[0].dstSubresource = { VK_IMAGE_ASPECT_PLANE_0_BIT, 0, 0, 1 };
+ regions[0].extent = { static_cast<u32>(source_context.width), static_cast<u32>(source_context.height), 1 };
+ regions[1].srcSubresource = { VK_IMAGE_ASPECT_PLANE_1_BIT, 0, 0, 1 };
+ regions[1].dstSubresource = { VK_IMAGE_ASPECT_PLANE_1_BIT, 0, 0, 1 };
+ regions[1].extent = { static_cast<u32>(source_context.width + 1) / 2, static_cast<u32>(source_context.height + 1) / 2, 1 };
+ vkCmdCopyImage(m_copy_command_buffer, source->img[0], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, destination->img[0], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 2, regions);
+
+ // The copy leaves the image ready for whichever device samples it next. FFmpeg shares its images between queue
+ // families whenever there are several, and otherwise leaves them owned by the one family.
+ auto releasing_queue_family = m_device.queue_families().size() > 1 ? VK_QUEUE_FAMILY_IGNORED : m_device.compute_queue_family();
+ VkImageMemoryBarrier2 export_barrier {};
+ export_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
+ export_barrier.srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT;
+ export_barrier.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
+ export_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
+ export_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
+ export_barrier.srcQueueFamilyIndex = releasing_queue_family;
+ export_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL;
+ export_barrier.image = destination->img[0];
+ export_barrier.subresourceRange = whole_image;
+
+ VkDependencyInfo export_dependency {};
+ export_dependency.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
+ export_dependency.imageMemoryBarrierCount = 1;
+ export_dependency.pImageMemoryBarriers = &export_barrier;
+ vkCmdPipelineBarrier2(m_copy_command_buffer, &export_dependency);
+
+ if (vkEndCommandBuffer(m_copy_command_buffer) != VK_SUCCESS)
+ return DecoderError::with_description(DecoderErrorCategory::Unknown, "Could not finish recording a frame copy"sv);
+
+ destination_vulkan_context.lock_frame(&destination_context, destination);
+ auto destination_semaphore = destination->sem[0];
+ auto copied_semaphore_value = ++destination->sem_value[0];
+ destination->layout[0] = VK_IMAGE_LAYOUT_GENERAL;
+ destination->access[0] = VK_ACCESS_2_MEMORY_READ_BIT;
+ // Mapping the frame only releases it to the external family if it has not been released already.
+ destination->queue_family[0] = VK_QUEUE_FAMILY_EXTERNAL;
+ destination_vulkan_context.unlock_frame(&destination_context, destination);
+
+ VkSemaphoreSubmitInfo wait_info {};
+ wait_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO;
+ wait_info.semaphore = decoded_semaphore;
+ wait_info.value = decoded_semaphore_value;
+ wait_info.stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
+
+ VkSemaphoreSubmitInfo signal_info {};
+ signal_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO;
+ signal_info.semaphore = destination_semaphore;
+ signal_info.value = copied_semaphore_value;
+ signal_info.stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
+
+ VkCommandBufferSubmitInfo command_buffer_info {};
+ command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO;
+ command_buffer_info.commandBuffer = m_copy_command_buffer;
+
+ VkSubmitInfo2 submit_info {};
+ submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2;
+ submit_info.waitSemaphoreInfoCount = 1;
+ submit_info.pWaitSemaphoreInfos = &wait_info;
+ submit_info.commandBufferInfoCount = 1;
+ submit_info.pCommandBufferInfos = &command_buffer_info;
+ submit_info.signalSemaphoreInfoCount = 1;
+ submit_info.pSignalSemaphoreInfos = &signal_info;
+
+ auto logical_device = m_device.logical_device();
+ auto queue_family = m_device.compute_queue_family();
+ VkQueue queue = VK_NULL_HANDLE;
+ vkGetDeviceQueue(logical_device, queue_family, 0, &queue);
+ m_device.lock_queue(queue_family);
+ auto submit_result = vkQueueSubmit2(queue, 1, &submit_info, VK_NULL_HANDLE);
+ m_device.unlock_queue(queue_family);
+ if (submit_result != VK_SUCCESS)
+ return DecoderError::with_description(DecoderErrorCategory::Unknown, "Could not submit a frame copy"sv);
+
+ // The command buffer is reused for the next copy, and the decoded image goes back to be decoded into.
+ if (!wait_for_semaphore(logical_device, destination_semaphore, copied_semaphore_value)) {
+ m_copy_stalled = true;
+ return DecoderError::with_description(DecoderErrorCategory::Unknown, "The Vulkan device failed or stalled while copying a decoded frame"sv);
+ }
+
+ free_copy.disarm();
+ return copy;
+}
+
DecoderErrorOr<NonnullRefPtr<VideoSurface>> FFmpegVulkanVideoDecoder::surface_for_exported_frame(AVFrame const& frame, AVFrame const& exported_frame)
{
auto const& frames_context = frames_context_of(frame);
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h
index 34eecb3acf9..5400390e82c 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h
@@ -82,6 +82,8 @@ private:
void complete_frames_until_exit();
DecoderErrorOr<DecodedOutput> complete_frame(DecodingFrame&);
+ DecoderErrorOr<void> prepare_to_copy_frames_from(AVFrame const&);
+ DecoderErrorOr<AVFrame*> copy_into_exportable_frame(AVFrame const&, VkSemaphore decoded_semaphore, u64 decoded_semaphore_value);
DecoderErrorOr<NonnullRefPtr<VideoSurface>> surface_for_exported_frame(AVFrame const& frame, AVFrame const& exported_frame);
FFmpegFunctions const& m_functions;
@@ -118,6 +120,16 @@ private:
// recognized when it comes back.
AVHWFramesContext const* m_frames_context_of_surfaces { nullptr };
HashMap<u64, NonnullRefPtr<VideoSurface>> m_surfaces_by_image;
+
+ // Only the completion thread uses these. The decoded frames' context is kept because the exportable frames'
+ // context creates its images with its video profile list.
+ AVBufferRef* m_frames_context_copied_from { nullptr };
+ AVBufferRef* m_exportable_frames_context { nullptr };
+ Vector<u64> m_exportable_drm_format_modifiers;
+ VkImageDrmFormatModifierListCreateInfoEXT m_exportable_drm_format_modifier_list {};
+ VkCommandPool m_copy_command_pool { VK_NULL_HANDLE };
+ VkCommandBuffer m_copy_command_buffer { VK_NULL_HANDLE };
+ bool m_copy_stalled { false };
};
}
diff --git a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp
index 6912920a07b..bfc1ef89d12 100644
--- a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp
+++ b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp
@@ -193,6 +193,7 @@ ErrorOr<NonnullOwnPtr<VulkanVideoDevice>> VulkanVideoDevice::create(Gfx::GpuDevi
auto transfer_family = first_queue_family_with(queue_families, VK_QUEUE_TRANSFER_BIT);
if (!decode_family.has_value() || !compute_family.has_value() || !transfer_family.has_value())
return Error::from_string_literal("The Vulkan device lacks a decode, compute or transfer queue");
+ device->m_compute_queue_family = *compute_family;
Vector<VkDeviceQueueCreateInfo> queue_infos;
float const queue_priority = 1.0f;
diff --git a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h
index fa45655e8bb..0ac46700da0 100644
--- a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h
+++ b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h
@@ -45,6 +45,7 @@ public:
VkDevice logical_device() const { return m_logical_device; }
u32 api_version() const { return m_api_version; }
ReadonlySpan<QueueFamily> queue_families() const { return m_queue_families; }
+ u32 compute_queue_family() const { return m_compute_queue_family; }
ReadonlySpan<char const*> enabled_extensions() const { return m_enabled_extensions; }
VkPhysicalDeviceFeatures2 const& enabled_features() const { return m_features; }
bool has_enabled_extension(StringView) const;
@@ -73,6 +74,7 @@ private:
VkPhysicalDevice m_physical_device { VK_NULL_HANDLE };
VkDevice m_logical_device { VK_NULL_HANDLE };
Vector<QueueFamily> m_queue_families;
+ u32 m_compute_queue_family { 0 };
// One for each of the decode, compute and transfer families, which may coincide.
Array<Mutex, 3> m_queue_mutexes;
Vector<char const*> m_enabled_extensions;