linux client: v4l2 decoder
This commit is contained in:
@@ -50,6 +50,14 @@ option(WIVRN_OPTIMIZE_SHADERS "Run spirv-opt on compiled shaders" ON)
|
||||
option(WIVRN_DEBUG_SHADERS "Compile shaders with debug information" OFF)
|
||||
option(WIVRN_ADDRESS_SANITIZER "Use address sanitizer" OFF)
|
||||
|
||||
# V4L2 decoder on by default on Linux AArch64
|
||||
set(_WIVRN_USE_V4L2_DEFAULT OFF)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" AND NOT ANDROID)
|
||||
set(_WIVRN_USE_V4L2_DEFAULT ON)
|
||||
endif()
|
||||
option(WIVRN_USE_V4L2 "Enable V4L2 hardware video decoder" ${_WIVRN_USE_V4L2_DEFAULT})
|
||||
unset(_WIVRN_USE_V4L2_DEFAULT)
|
||||
|
||||
if(WIVRN_OPTIMIZE_SHADERS AND WIVRN_DEBUG_SHADERS)
|
||||
message(FATAL_ERROR "WIVRN_OPTIMIZE_SHADERS and WIVRN_DEBUG_SHADERS are incompatible")
|
||||
endif()
|
||||
|
||||
@@ -113,6 +113,13 @@ else()
|
||||
file(GLOB PLATFORM_SOURCE CONFIGURE_DEPENDS
|
||||
"decoder/ffmpeg/*.cpp")
|
||||
|
||||
if(WIVRN_USE_V4L2)
|
||||
file(GLOB CLIENT_V4L2_SOURCE CONFIGURE_DEPENDS
|
||||
"decoder/v4l2/*.cpp")
|
||||
list(APPEND PLATFORM_SOURCE ${CLIENT_V4L2_SOURCE})
|
||||
target_compile_definitions(wivrn PRIVATE WIVRN_USE_V4L2)
|
||||
endif()
|
||||
|
||||
if(WIVRN_USE_PIPEWIRE)
|
||||
file(GLOB CLIENT_PIPEWIRE_SOURCE CONFIGURE_DEPENDS
|
||||
"audio/linux/*.cpp")
|
||||
|
||||
@@ -838,6 +838,14 @@ void application::initialize_vulkan()
|
||||
optional_device_extensions.emplace(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
|
||||
optional_device_extensions.emplace(VK_EXT_FRAGMENT_DENSITY_MAP_EXTENSION_NAME);
|
||||
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
// V4L2 decoders expose their output through DMA-buf
|
||||
optional_device_extensions.emplace(VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME);
|
||||
optional_device_extensions.emplace(VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME);
|
||||
optional_device_extensions.emplace(VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME);
|
||||
optional_device_extensions.emplace(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME);
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
vk_device_extensions.push_back(VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME);
|
||||
vk_device_extensions.push_back(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME);
|
||||
|
||||
@@ -22,8 +22,13 @@
|
||||
#include "decoder/android/android_decoder.h"
|
||||
#else
|
||||
#include "decoder/ffmpeg/ffmpeg_decoder.h"
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
#include "decoder/v4l2/v4l2_decoder.h"
|
||||
#endif
|
||||
#endif
|
||||
#include "decoder/raw_decoder.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <algorithm>
|
||||
|
||||
wivrn::decoder::~decoder() = default;
|
||||
|
||||
@@ -50,13 +55,22 @@ std::shared_ptr<wivrn::decoder> wivrn::decoder::make(
|
||||
scene,
|
||||
acc);
|
||||
#else
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
if (wivrn::v4l2::decoder::available_for(description.codec[stream_index]))
|
||||
{
|
||||
try
|
||||
{
|
||||
return std::make_shared<wivrn::v4l2::decoder>(
|
||||
device, phys_dev, vk_queue_family_index, description, stream_index, scene, acc);
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
spdlog::warn("V4L2 iris decoder initialization failed: {}. Falling back to FFmpeg.", e.what());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return std::make_shared<wivrn::ffmpeg::decoder>(
|
||||
device,
|
||||
phys_dev,
|
||||
description,
|
||||
stream_index,
|
||||
scene,
|
||||
acc);
|
||||
device, phys_dev, description, stream_index, scene, acc);
|
||||
#endif
|
||||
case raw:
|
||||
return std::make_shared<wivrn::raw_decoder>(
|
||||
@@ -77,7 +91,14 @@ static std::vector<wivrn::video_codec> supported_codecs_()
|
||||
#ifdef __ANDROID__
|
||||
wivrn::android::decoder::supported_codecs(res);
|
||||
#else
|
||||
wivrn::ffmpeg::decoder::supported_codecs(res);
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
wivrn::v4l2::decoder::supported_codecs(res);
|
||||
#endif
|
||||
std::vector<wivrn::video_codec> software_codecs;
|
||||
wivrn::ffmpeg::decoder::supported_codecs(software_codecs);
|
||||
for (auto codec: software_codecs)
|
||||
if (not std::ranges::contains(res, codec))
|
||||
res.push_back(codec);
|
||||
#endif
|
||||
res.push_back(wivrn::video_codec::raw);
|
||||
return res;
|
||||
|
||||
@@ -46,6 +46,11 @@ public:
|
||||
vk::ImageLayout & current_layout;
|
||||
vk::Semaphore semaphore = nullptr;
|
||||
uint64_t * semaphore_val = nullptr;
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
// DMA-buf images from V4L2 require queue-family ownership
|
||||
// transfers between Vulkan and external engine
|
||||
uint32_t foreign_queue_family = vk::QueueFamilyIgnored;
|
||||
#endif
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2026 galister <galister-dev@pm.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "decoder/decoder.h"
|
||||
#include "wivrn_sockets.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <sys/mman.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
namespace scenes
|
||||
{
|
||||
class stream;
|
||||
}
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class shard_accumulator;
|
||||
}
|
||||
|
||||
namespace wivrn::v4l2
|
||||
{
|
||||
class decoder : public wivrn::decoder
|
||||
{
|
||||
private:
|
||||
struct v4l2_blit_handle;
|
||||
|
||||
struct mapped_plane
|
||||
{
|
||||
void * address = nullptr;
|
||||
size_t length = 0;
|
||||
};
|
||||
|
||||
struct output_buffer
|
||||
{
|
||||
std::vector<mapped_plane> planes;
|
||||
~output_buffer()
|
||||
{
|
||||
for (auto & p: planes)
|
||||
if (p.address && p.address != MAP_FAILED)
|
||||
munmap(p.address, p.length);
|
||||
}
|
||||
};
|
||||
|
||||
struct capture_buffer
|
||||
{
|
||||
std::vector<size_t> plane_lengths;
|
||||
vk::raii::DeviceMemory memory = nullptr;
|
||||
vk::raii::Image image = nullptr;
|
||||
vk::raii::ImageView image_view = nullptr;
|
||||
vk::ImageLayout current_layout = vk::ImageLayout::eUndefined;
|
||||
bool held_by_vulkan = false;
|
||||
};
|
||||
|
||||
struct pending_frame
|
||||
{
|
||||
uint64_t timestamp = 0;
|
||||
wivrn::from_headset::feedback feedback;
|
||||
wivrn::to_headset::video_stream_data_shard::view_info_t view_info;
|
||||
};
|
||||
|
||||
struct ready_output
|
||||
{
|
||||
uint32_t buffer_index;
|
||||
size_t bytes_used;
|
||||
pending_frame pending;
|
||||
};
|
||||
|
||||
struct assembling_frame
|
||||
{
|
||||
uint32_t buffer_index;
|
||||
size_t bytes_used;
|
||||
uint64_t frame_index;
|
||||
};
|
||||
|
||||
vk::raii::Device & device;
|
||||
vk::raii::PhysicalDevice & physical_device;
|
||||
vk::raii::SamplerYcbcrConversion ycbcr_conversion = nullptr;
|
||||
vk::raii::Sampler ycbcr_sampler = nullptr;
|
||||
vk::Format capture_vk_format = vk::Format::eUndefined;
|
||||
|
||||
wivrn::fd_base fd;
|
||||
std::vector<output_buffer> output_buffers;
|
||||
std::vector<capture_buffer> capture_buffers;
|
||||
std::vector<unsigned int> free_output_buffers;
|
||||
std::deque<pending_frame> pending_frames;
|
||||
|
||||
std::deque<ready_output> input_queue;
|
||||
std::mutex input_mutex;
|
||||
std::optional<assembling_frame> assembling;
|
||||
std::optional<uint64_t> dropped_frame_index;
|
||||
std::mutex output_mutex;
|
||||
std::deque<unsigned int> recycle_capture_queue;
|
||||
std::deque<unsigned int> recycle_capture_local;
|
||||
std::mutex recycle_mutex;
|
||||
wivrn::fd_base wake_fd;
|
||||
std::jthread worker;
|
||||
std::exception_ptr worker_exception;
|
||||
std::mutex worker_exception_mutex;
|
||||
|
||||
uint64_t next_timestamp = 1;
|
||||
unsigned int queued_output_count = 0;
|
||||
uint32_t capture_width = 0;
|
||||
uint32_t capture_height = 0;
|
||||
uint32_t capture_bytesperline = 0;
|
||||
uint32_t capture_sizeimage = 0;
|
||||
uint32_t capture_pixelformat = 0;
|
||||
uint32_t capture_num_planes = 0;
|
||||
bool output_streaming = false;
|
||||
bool capture_streaming = false;
|
||||
|
||||
std::weak_ptr<scenes::stream> weak_scene;
|
||||
shard_accumulator * accumulator;
|
||||
const vk::Extent2D extent;
|
||||
const wivrn::video_codec codec_type;
|
||||
const uint32_t vk_queue_family_index;
|
||||
|
||||
void setup_device(wivrn::fd_base device_fd);
|
||||
void setup_output_queue();
|
||||
void setup_capture_queue();
|
||||
void teardown_capture_queue();
|
||||
void setup_ycbcr_sampler();
|
||||
void import_capture_buffer(unsigned int index, const std::vector<size_t> & plane_lengths);
|
||||
void initialize_capture_ownership();
|
||||
void queue_capture_buffer(unsigned int index);
|
||||
void release_capture_buffer(unsigned int index);
|
||||
void recycle_capture_buffers();
|
||||
void handle_events();
|
||||
void reclaim_output_buffers();
|
||||
void drain_capture();
|
||||
void queue_ready_frames();
|
||||
void queue_output_buffer(const ready_output & ready);
|
||||
void abandon_assembling_frame();
|
||||
void worker_loop(std::stop_token stop_token);
|
||||
void wake_worker();
|
||||
void rethrow_worker_exception();
|
||||
void submit_capture(unsigned int capture_index, uint64_t timestamp);
|
||||
pending_frame take_pending(uint64_t timestamp);
|
||||
|
||||
public:
|
||||
decoder(vk::raii::Device & device,
|
||||
vk::raii::PhysicalDevice & physical_device,
|
||||
uint32_t vk_queue_family_index,
|
||||
const wivrn::to_headset::video_stream_description & description,
|
||||
uint8_t stream_index,
|
||||
std::weak_ptr<scenes::stream> scene,
|
||||
shard_accumulator * accumulator);
|
||||
~decoder() override;
|
||||
|
||||
void push_data(std::span<std::span<const uint8_t>> data, uint64_t frame_index, bool partial) override;
|
||||
void frame_completed(
|
||||
const wivrn::from_headset::feedback & feedback,
|
||||
const wivrn::to_headset::video_stream_data_shard::view_info_t & view_info) override;
|
||||
|
||||
vk::Sampler sampler() override
|
||||
{
|
||||
return *ycbcr_sampler;
|
||||
}
|
||||
|
||||
static bool available_for(wivrn::video_codec codec);
|
||||
static void supported_codecs(std::vector<wivrn::video_codec> & result);
|
||||
};
|
||||
} // namespace wivrn::v4l2
|
||||
@@ -920,7 +920,37 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
|
||||
use_alpha = blit_handle->view_info.alpha;
|
||||
|
||||
if (blit_handle->current_layout == vk::ImageLayout::eUndefined)
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
if (blit_handle->foreign_queue_family != vk::QueueFamilyIgnored)
|
||||
{
|
||||
// decoder completed its write before handing us this image
|
||||
// acquire ownership from the external media engine before sampling it
|
||||
vk::ImageMemoryBarrier barrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eNone,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = blit_handle->current_layout,
|
||||
.newLayout = vk::ImageLayout::eGeneral,
|
||||
.srcQueueFamilyIndex = blit_handle->foreign_queue_family,
|
||||
.dstQueueFamilyIndex = queue_family_index,
|
||||
.image = blit_handle->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.levelCount = 1,
|
||||
.layerCount = 1,
|
||||
},
|
||||
};
|
||||
|
||||
command_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe,
|
||||
vk::PipelineStageFlagBits::eFragmentShader,
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
barrier);
|
||||
blit_handle->current_layout = vk::ImageLayout::eGeneral;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (blit_handle->current_layout == vk::ImageLayout::eUndefined)
|
||||
{
|
||||
vk::ImageMemoryBarrier barrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eNone,
|
||||
@@ -1050,6 +1080,41 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
{bias, bias, bias, 0.},
|
||||
image_index);
|
||||
|
||||
#ifdef WIVRN_USE_V4L2
|
||||
// the blit handles keep CAPTURE buffers alive until this cmd buffer's fence has completed
|
||||
inplace_vector<vk::ImageMemoryBarrier, decoder_count> foreign_release_barriers;
|
||||
for (const auto & handle: current_blit_handles)
|
||||
{
|
||||
if (handle && handle->foreign_queue_family != vk::QueueFamilyIgnored)
|
||||
{
|
||||
foreign_release_barriers.push_back({
|
||||
.srcAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.dstAccessMask = vk::AccessFlagBits::eNone,
|
||||
.oldLayout = handle->current_layout,
|
||||
.newLayout = vk::ImageLayout::eGeneral,
|
||||
.srcQueueFamilyIndex = queue_family_index,
|
||||
.dstQueueFamilyIndex = handle->foreign_queue_family,
|
||||
.image = handle->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.levelCount = 1,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
handle->current_layout = vk::ImageLayout::eGeneral;
|
||||
}
|
||||
}
|
||||
if (!foreign_release_barriers.empty())
|
||||
{
|
||||
command_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eFragmentShader,
|
||||
vk::PipelineStageFlagBits::eBottomOfPipe,
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
foreign_release_barriers);
|
||||
}
|
||||
#endif
|
||||
|
||||
command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 1);
|
||||
|
||||
command_buffer.end();
|
||||
|
||||
Reference in New Issue
Block a user