Add support for raw video

This commit is contained in:
Patrick Nicolas
2025-09-26 16:33:01 +02:00
parent 7861d508c7
commit 6932f9461d
20 changed files with 726 additions and 25 deletions
+4
View File
@@ -70,6 +70,7 @@ static void split_bitrate(std::vector<wivrn::encoder_settings> & encoders, uint6
break;
case wivrn::h265:
case wivrn::av1:
case wivrn::raw:
break;
}
encoder.bitrate = w;
@@ -296,6 +297,9 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
config.codec = h264; // this will fail if 10-bit is enabled
#endif
if (config.name == encoder_raw)
config.codec = raw;
if (not config.codec)
config.codec = bit_depth == 10 ? h265 : h264;
}
@@ -58,6 +58,8 @@ const char * encoder(video_codec codec)
return "hevc_vaapi";
case video_codec::av1:
return "av1_vaapi";
case video_codec::raw:
break;
}
throw std::runtime_error("invalid codec " + std::to_string(int(codec)));
}
@@ -239,6 +241,8 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
case video_codec::av1:
encoder_ctx->profile = FF_PROFILE_AV1_MAIN;
break;
case video_codec::raw:
throw std::runtime_error("raw codec not supported");
}
for (auto option: settings.options)
{
+12
View File
@@ -42,6 +42,7 @@
#include "video_encoder_vulkan_h264.h"
// #include "video_encoder_vulkan_h265.h"
#endif
#include "video_encoder_raw.h"
namespace wivrn
{
@@ -126,6 +127,8 @@ std::unique_ptr<video_encoder> video_encoder::create(
// break;
case video_codec::av1:
throw std::runtime_error("av1 not supported for vulkan video encode");
case video_codec::raw:
throw std::runtime_error("raw codec only supported on raw encoder");
}
#else
throw std::runtime_error("Vulkan video encode not enabled");
@@ -155,6 +158,12 @@ std::unique_ptr<video_encoder> video_encoder::create(
throw std::runtime_error("vaapi support not enabled");
#endif
}
if (settings.encoder_name == encoder_raw)
{
res = std::make_unique<video_encoder_raw>(wivrn_vk, settings, fps, stream_idx);
}
if (not res)
throw std::runtime_error("Failed to create encoder " + settings.encoder_name);
@@ -174,6 +183,9 @@ std::unique_ptr<video_encoder> video_encoder::create(
case av1:
file += ".av1";
break;
case raw:
file += ".yuv";
break;
}
res->video_dump.open(file);
}
+1
View File
@@ -43,6 +43,7 @@ inline const char * encoder_nvenc = "nvenc";
inline const char * encoder_vaapi = "vaapi";
inline const char * encoder_x264 = "x264";
inline const char * encoder_vulkan = "vulkan";
inline const char * encoder_raw = "raw";
class video_encoder
{
+4
View File
@@ -74,6 +74,8 @@ static auto encode_guid(video_codec codec)
return NV_ENC_CODEC_HEVC_GUID;
case av1:
return NV_ENC_CODEC_AV1_GUID;
case raw:
break;
}
throw std::out_of_range("Invalid codec " + std::to_string(codec));
}
@@ -164,6 +166,8 @@ video_encoder_nvenc::video_encoder_nvenc(
params.encodeCodecConfig.av1Config.maxNumRefFramesInDPB = 0;
params.encodeCodecConfig.av1Config.idrPeriod = NVENC_INFINITE_GOPLENGTH;
break;
case video_codec::raw:
throw std::runtime_error("raw codec not supported for nvenc");
}
NV_ENC_INITIALIZE_PARAMS params2{
+121
View File
@@ -0,0 +1,121 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "video_encoder_raw.h"
#include "encoder/encoder_settings.h"
#include "utils/wivrn_vk_bundle.h"
wivrn::video_encoder_raw::video_encoder_raw(
wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
uint8_t stream_idx) :
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true)
{
if (settings.bit_depth != 8)
throw std::runtime_error("Raw encoding is only supported for 8 bit");
rect = vk::Rect2D{
.offset = {
.x = settings.offset_x,
.y = settings.offset_y,
},
.extent = {
.width = settings.width,
.height = settings.height,
},
};
vk::DeviceSize buffer_size = settings.width * settings.height;
switch (settings.channels)
{
case to_headset::video_stream_description::channels_t::colour:
buffer_size += buffer_size / 2;
break;
case to_headset::video_stream_description::channels_t::alpha:
break;
}
for (auto & slot: buffers)
{
slot = buffer_allocation(
vk.device,
{
.size = buffer_size,
.usage = vk::BufferUsageFlagBits::eTransferDst,
},
{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
},
"raw stream buffer");
}
}
std::pair<bool, vk::Semaphore> wivrn::video_encoder_raw::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t)
{
std::array regions{
vk::BufferImageCopy{
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = uint32_t(channels),
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x,
.y = rect.offset.y,
},
.imageExtent = {
.width = rect.extent.width,
.height = rect.extent.height,
.depth = 1,
},
},
vk::BufferImageCopy{
.bufferOffset = rect.extent.width * rect.extent.height,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = uint32_t(channels),
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x / 2,
.y = rect.offset.y / 2,
},
.imageExtent = {
.width = rect.extent.width / 2,
.height = rect.extent.height / 2,
.depth = 1,
},
},
};
cmd_buf.copyImageToBuffer(
y_cbcr,
vk::ImageLayout::eTransferSrcOptimal,
buffers[slot],
std::span(regions.data(),
channels == to_headset::video_stream_description::channels_t::colour ? 2 : 1));
return {false, nullptr};
}
std::optional<wivrn::video_encoder::data> wivrn::video_encoder_raw::encode(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot)
{
return wivrn::video_encoder::data{
.encoder = this,
.span = std::span<uint8_t>((uint8_t *)buffers[slot].map(), buffers[slot].info().size),
};
}
+41
View File
@@ -0,0 +1,41 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "video_encoder.h"
#include "vk/allocation.h"
#include <array>
namespace wivrn
{
class video_encoder_raw : public video_encoder
{
std::array<buffer_allocation, num_slots> buffers;
vk::Rect2D rect;
public:
video_encoder_raw(wivrn_vk_bundle & vk, encoder_settings & settings, float fps, uint8_t stream_idx);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot) override;
};
} // namespace wivrn