Truly independent web browser
LibMediaCodecs+VideoDecoderServer: Decode H.264 and HEVC with Vulkan
Zaggy1024bc1e185b2d95
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.hindex 10004e1b77e..031bf8bdbbd 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegFunctions.h@@ -36,6 +36,7 @@ static_assert(sizeof(void*) != 8 || (sizeof(AVChannelLayout) == 24 && offsetof(A#define FFMPEG_ENUMERATE_FUNCTIONS(F) \F(avcodec_version) \+ F(avutil_version) \F(avcodec_find_decoder) \F(avcodec_find_decoder_by_name) \F(avcodec_descriptor_get_by_name) \diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cppindex c9ae9773159..30f23ea5d3b 100644--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp@@ -5,6 +5,8 @@*/#include <AK/Atomic.h>+#include <AK/Diagnostics.h>+#include <AK/HashMap.h>#include <AK/NeverDestroyed.h>#include <AK/ScopeGuard.h>#include <LibIPC/File.h>@@ -16,6 +18,10 @@#include <LibThreading/Thread.h>#include <string.h>+#if defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)+# include <LibMediaCodecs/FFmpeg/SystemFFmpeg.h>+#endif+extern "C" {#include <libavutil/hwcontext_drm.h>#include <libavutil/hwcontext_vulkan.h>@@ -45,6 +51,10 @@ char const* decoder_name_for(CodecID codec_id)return "vp9";case CodecID::AV1:return "av1";+ case CodecID::H264:+ return "h264";+ case CodecID::H265:+ return "hevc";default:return nullptr;}@@ -57,17 +67,32 @@ StringView decode_extension_for(CodecID codec_id)return VK_KHR_VIDEO_DECODE_VP9_EXTENSION_NAME ""sv;case CodecID::AV1:return VK_KHR_VIDEO_DECODE_AV1_EXTENSION_NAME ""sv;+ case CodecID::H264:+ return VK_KHR_VIDEO_DECODE_H264_EXTENSION_NAME ""sv;+ case CodecID::H265:+ return VK_KHR_VIDEO_DECODE_H265_EXTENSION_NAME ""sv;default:VERIFY_NOT_REACHED();}}+// We only ship decoders for royalty-free codecs, so H.264 and HEVC decode through the system's FFmpeg. The decoder+// reaches into FFmpeg's hardware structs, so that library is only used when they are laid out as in the bundled one.FFmpegFunctions const* ffmpeg_for(CodecID codec_id){switch (codec_id) {case CodecID::VP9:case CodecID::AV1:return &FFmpegFunctions::bundled();+#if defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)+ case CodecID::H264:+ case CodecID::H265: {+ auto const* library = SystemFFmpeg::the();+ if (library == nullptr || !library->shares_bundled_struct_layouts())+ return nullptr;+ return &library->functions();+ }+#endifdefault:return nullptr;}@@ -101,15 +126,26 @@ Optional<VkVideoComponentBitDepthFlagBitsKHR> component_bit_depth_for(u8 bit_dep}}-// Every decoder in the process shares one device context around the process's device, which lives as long as it does.+void lock_queue(AVHWDeviceContext*, u32 queue_family, u32)+{+ VulkanVideoDevice::the()->lock_queue(queue_family);+}++void unlock_queue(AVHWDeviceContext*, u32 queue_family, u32)+{+ VulkanVideoDevice::the()->unlock_queue(queue_family);+}++// The decoders using one FFmpeg share one device context around the process's device, which lives as long as it+// does. A context only works with the FFmpeg that made it, so each FFmpeg gets its own.DecoderErrorOr<AVBufferRef*> device_context_for(FFmpegFunctions const& functions, VulkanVideoDevice& device){static NeverDestroyed<Mutex> mutex;- static AVBufferRef* device_context_reference = nullptr;+ static NeverDestroyed<HashMap<FFmpegFunctions const*, AVBufferRef*>> device_context_references;MutexLocker locker { *mutex };- if (device_context_reference != nullptr)- return device_context_reference;+ if (auto reference = device_context_references->get(&functions); reference.has_value())+ return *reference;auto* reference = functions.av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VULKAN);if (reference == nullptr)@@ -131,12 +167,15 @@ DecoderErrorOr<AVBufferRef*> device_context_for(FFmpegFunctions const& functionsqueue_family.num = 1;queue_family.flags = static_cast<VkQueueFlagBits>(family.flags);}+ // Each context would otherwise keep its own locks on queues that the others submit to as well.+ AK_IGNORE_DIAGNOSTIC("-Wdeprecated-declarations", vulkan_context.lock_queue = lock_queue;)+ AK_IGNORE_DIAGNOSTIC("-Wdeprecated-declarations", vulkan_context.unlock_queue = unlock_queue;)if (functions.av_hwdevice_ctx_init(reference) < 0)return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "FFmpeg could not use the Vulkan device"sv);+ DECODER_TRY_ALLOC(device_context_references->try_set(&functions, reference));free_reference.disarm();- device_context_reference = reference;return reference;}@@ -216,10 +255,18 @@ Optional<DecoderCapabilities> FFmpegVulkanVideoDecoder::capabilities(ParsedCodecvp9_profile.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_PROFILE_INFO_KHR;VkVideoDecodeAV1ProfileInfoKHR av1_profile {};av1_profile.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR;+ VkVideoDecodeH264ProfileInfoKHR h264_profile {};+ h264_profile.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PROFILE_INFO_KHR;+ VkVideoDecodeH265ProfileInfoKHR h265_profile {};+ h265_profile.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_PROFILE_INFO_KHR;VkVideoDecodeVP9CapabilitiesKHR vp9_capabilities {};vp9_capabilities.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_CAPABILITIES_KHR;VkVideoDecodeAV1CapabilitiesKHR av1_capabilities {};av1_capabilities.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_CAPABILITIES_KHR;+ VkVideoDecodeH264CapabilitiesKHR h264_capabilities {};+ h264_capabilities.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_CAPABILITIES_KHR;+ VkVideoDecodeH265CapabilitiesKHR h265_capabilities {};+ h265_capabilities.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_CAPABILITIES_KHR;VkVideoProfileInfoKHR profile_info {};profile_info.sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_INFO_KHR;@@ -258,6 +305,48 @@ Optional<DecoderCapabilities> FFmpegVulkanVideoDecoder::capabilities(ParsedCodecdecode_capabilities.pNext = &av1_capabilities;break;}+ case CodecID::H264: {+ // Without a profile, ask for High, which can decode the other two.+ h264_profile.stdProfileIdc = STD_VIDEO_H264_PROFILE_IDC_HIGH;+ if (auto parameters = codec.h264_parameters(); parameters.has_value()) {+ switch (parameters->profile().value_or(Codecs::H264::Profile::High10)) {+ case Codecs::H264::Profile::Baseline:+ h264_profile.stdProfileIdc = STD_VIDEO_H264_PROFILE_IDC_BASELINE;+ break;+ case Codecs::H264::Profile::Main:+ h264_profile.stdProfileIdc = STD_VIDEO_H264_PROFILE_IDC_MAIN;+ break;+ case Codecs::H264::Profile::High:+ break;+ default:+ return {};+ }+ }+ h264_profile.pictureLayout = VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_PROGRESSIVE_KHR;+ profile_info.pNext = &h264_profile;+ profile_info.videoCodecOperation = VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR;+ decode_capabilities.pNext = &h264_capabilities;+ break;+ }+ case CodecID::H265: {+ h265_profile.stdProfileIdc = STD_VIDEO_H265_PROFILE_IDC_MAIN;+ if (auto parameters = codec.h265_parameters(); parameters.has_value()) {+ switch (parameters->profile().value_or(Codecs::H265::Profile::Main12)) {+ case Codecs::H265::Profile::Main:+ break;+ case Codecs::H265::Profile::Main10:+ h265_profile.stdProfileIdc = STD_VIDEO_H265_PROFILE_IDC_MAIN_10;+ bit_depth = 10;+ break;+ default:+ return {};+ }+ }+ profile_info.pNext = &h265_profile;+ profile_info.videoCodecOperation = VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR;+ decode_capabilities.pNext = &h265_capabilities;+ break;+ }default:VERIFY_NOT_REACHED();}diff --git a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cpp b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cppindex 5a4737b5ecf..f49971778cf 100644--- a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cpp+++ b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cpp@@ -142,4 +142,9 @@ bool SystemFFmpeg::provides_decoder_missing_from_bundle() const});}+bool SystemFFmpeg::shares_bundled_struct_layouts() const+{+ return m_functions->avcodec_version() >> 16 == LIBAVCODEC_VERSION_MAJOR && m_functions->avutil_version() >> 16 == LIBAVUTIL_VERSION_MAJOR;+}+}diff --git a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.h b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.hindex 46fc6fa2bb6..05daf27c315 100644--- a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.h+++ b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.h@@ -30,6 +30,9 @@ public:unsigned major() const { return m_major; }bool has_decoder(CodecID) const;bool provides_decoder_missing_from_bundle() const;+ // Within a major, FFmpeg only appends to its structs, so code built against the bundled headers can reach into this+ // library's structs when both of its majors match the bundled ones.+ bool shares_bundled_struct_layouts() const;private:void* m_library_handle;diff --git a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cppindex 6c082e8fff1..6912920a07b 100644--- a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp+++ b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.cpp@@ -25,6 +25,8 @@ constexpr Array required_extensions {constexpr Array optional_extensions {VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,+ VK_KHR_VIDEO_DECODE_H264_EXTENSION_NAME,+ VK_KHR_VIDEO_DECODE_H265_EXTENSION_NAME,VK_KHR_VIDEO_DECODE_VP9_EXTENSION_NAME,VK_KHR_VIDEO_DECODE_AV1_EXTENSION_NAME,VK_KHR_VIDEO_MAINTENANCE_1_EXTENSION_NAME,@@ -236,6 +238,25 @@ bool VulkanVideoDevice::has_enabled_extension(StringView name) constreturn any_of(m_enabled_extensions, [&](auto enabled) { return StringView { enabled, strlen(enabled) } == name; });}+Mutex& VulkanVideoDevice::queue_mutex(u32 family_index)+{+ for (size_t i = 0; i < m_queue_families.size(); i++) {+ if (m_queue_families[i].index == family_index)+ return m_queue_mutexes[i];+ }+ VERIFY_NOT_REACHED();+}++void VulkanVideoDevice::lock_queue(u32 family_index)+{+ queue_mutex(family_index).lock();+}++void VulkanVideoDevice::unlock_queue(u32 family_index)+{+ queue_mutex(family_index).unlock();+}+ErrorOr<void> VulkanVideoDevice::video_capabilities(VkVideoProfileInfoKHR const& profile, VkVideoCapabilitiesKHR& capabilities) const{if (m_get_physical_device_video_capabilities(m_physical_device, &profile, &capabilities) != VK_SUCCESS)diff --git a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.hindex 94a0f2d993f..fa45655e8bb 100644--- a/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h+++ b/Libraries/LibMediaCodecs/Vulkan/VulkanVideoDevice.h@@ -8,7 +8,9 @@#ifdef USE_VULKAN_DMABUF_IMAGES+# include <AK/Array.h># include <AK/Error.h>+# include <AK/Mutex.h># include <AK/NonnullOwnPtr.h># include <AK/Vector.h># include <LibGfx/GpuDeviceIdentity.h>@@ -47,6 +49,11 @@ public:VkPhysicalDeviceFeatures2 const& enabled_features() const { return m_features; }bool has_enabled_extension(StringView) const;+ // A queue takes one submission at a time, and every FFmpeg in the process submits to this device's queues. Each+ // family has one queue.+ void lock_queue(u32 family_index);+ void unlock_queue(u32 family_index);+// Fills in the capabilities, and whatever codec-specific capabilities are chained to them, for a video profile.ErrorOr<void> video_capabilities(VkVideoProfileInfoKHR const&, VkVideoCapabilitiesKHR&) const;@@ -59,11 +66,15 @@ private:static ErrorOr<NonnullOwnPtr<VulkanVideoDevice>> create(Gfx::GpuDeviceIdentity const&);+ Mutex& queue_mutex(u32 family_index);+u32 m_api_version { 0 };VkInstance m_instance { VK_NULL_HANDLE };VkPhysicalDevice m_physical_device { VK_NULL_HANDLE };VkDevice m_logical_device { VK_NULL_HANDLE };Vector<QueueFamily> m_queue_families;+ // 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;VkPhysicalDeviceFeatures2 m_features {};diff --git a/Services/VideoDecoderServer/main.cpp b/Services/VideoDecoderServer/main.cppindex e94279c22bd..7fac63835a8 100644--- a/Services/VideoDecoderServer/main.cpp+++ b/Services/VideoDecoderServer/main.cpp@@ -21,6 +21,10 @@#include <VideoDecoderServer/ControlConnection.h>#include <VideoDecoderServer/Sandbox.h>+#if defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)+# include <LibMediaCodecs/FFmpeg/SystemFFmpeg.h>+#endif+static Media::DecoderErrorOr<NonnullOwnPtr<Media::VideoDecoder>> create_software_video_decoder(Media::CodecID codec_id, ReadonlyBytes codec_initialization_data){return NonnullOwnPtr<Media::VideoDecoder> { TRY(Media::FFmpeg::FFmpegVideoDecoder::try_create(codec_id, codec_initialization_data)) };@@ -91,6 +95,12 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)}#endif+#if defined(USE_VULKAN_DMABUF_IMAGES) && defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)+ // H.264 and HEVC decode through the system library, whose dependencies make syscalls the sandbox will not allow.+ if (Media::VulkanVideoDevice::the() != nullptr)+ (void)Media::FFmpeg::SystemFFmpeg::the();+#endif+Vector<Media::VideoDecoderRegistration> hosted_decoders;auto hardware_decoders = Media::hardware_video_decoders();hosted_decoders.append(hardware_decoders.data(), hardware_decoders.size());