wip: set bitrate at runtime
This commit is contained in:
@@ -76,7 +76,8 @@ static void split_bitrate(std::vector<wivrn::encoder_settings> & encoders, uint6
|
||||
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
encoder.bitrate = encoder.bitrate * bitrate / total_weight;
|
||||
encoder.bitrate_multiplier = encoder.bitrate / total_weight;
|
||||
encoder.bitrate = encoder.bitrate_multiplier * bitrate;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ struct encoder_settings : public to_headset::video_stream_description::item
|
||||
// encoder identifier, such as nvenc, vaapi or x264
|
||||
std::string encoder_name;
|
||||
uint64_t bitrate; // bit/s
|
||||
double bitrate_multiplier; // encoder bitrate / global bitrate
|
||||
std::map<std::string, std::string> options; // additional encoder-specific configuration
|
||||
// encoders in the same group are executed in sequence
|
||||
int group = 0;
|
||||
|
||||
@@ -68,6 +68,14 @@ bool video_encoder_ffmpeg::once = set_log_level();
|
||||
|
||||
std::optional<wivrn::video_encoder::data> video_encoder_ffmpeg::encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t slot)
|
||||
{
|
||||
if (auto bitrate = pending_bitrate.exchange(BITRATE_UNCHANGED))
|
||||
{
|
||||
U_LOG_E("Reconfigure FFMPEG encoder");
|
||||
idr = true;
|
||||
encoder_ctx->bit_rate = bitrate;
|
||||
encoder_ctx->rc_max_rate = bitrate;
|
||||
}
|
||||
|
||||
push_frame(idr, target_timestamp, slot);
|
||||
std::shared_ptr<AVPacket> enc_pkt(av_packet_alloc(), [](AVPacket * d) { av_packet_free(&d); });
|
||||
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
|
||||
|
||||
@@ -38,8 +38,8 @@ public:
|
||||
};
|
||||
|
||||
protected:
|
||||
video_encoder_ffmpeg(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels) :
|
||||
wivrn::video_encoder(stream_idx, channels, true) {}
|
||||
video_encoder_ffmpeg(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier) :
|
||||
wivrn::video_encoder(stream_idx, channels, bitrate_multiplier, true) {}
|
||||
|
||||
virtual void push_frame(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot) = 0;
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
|
||||
wivrn::encoder_settings & settings,
|
||||
float fps,
|
||||
uint8_t stream_idx) :
|
||||
video_encoder_ffmpeg(stream_idx, settings.channels),
|
||||
video_encoder_ffmpeg(stream_idx, settings.channels, settings.bitrate_multiplier),
|
||||
synchronization2(vk.vk.features.synchronization_2)
|
||||
{
|
||||
auto drm_hw_ctx = make_drm_hw_ctx(vk.physical_device, settings.device);
|
||||
|
||||
@@ -208,9 +208,10 @@ std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> video_encod
|
||||
|
||||
static const uint64_t idr_throttle = 100;
|
||||
|
||||
video_encoder::video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, bool async_send) :
|
||||
video_encoder::video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier, bool async_send) :
|
||||
stream_idx(stream_idx),
|
||||
channels(channels),
|
||||
bitrate_multiplier(bitrate_multiplier),
|
||||
last_idr_frame(-idr_throttle),
|
||||
shared_sender(async_send ? sender::get() : nullptr)
|
||||
{}
|
||||
@@ -232,6 +233,11 @@ void video_encoder::reset()
|
||||
sync_needed = true;
|
||||
}
|
||||
|
||||
void video_encoder::set_bitrate(int bitrate_bps)
|
||||
{
|
||||
pending_bitrate = bitrate_bps;
|
||||
}
|
||||
|
||||
std::pair<bool, vk::Semaphore> video_encoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint64_t frame_index)
|
||||
{
|
||||
// Wait for encoder to be done
|
||||
|
||||
@@ -33,6 +33,8 @@
|
||||
#include <thread>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#define BITRATE_UNCHANGED 0
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
@@ -75,6 +77,7 @@ public:
|
||||
const uint8_t stream_idx;
|
||||
const to_headset::video_stream_description::channels_t channels;
|
||||
static const uint8_t num_slots = 2;
|
||||
const double bitrate_multiplier;
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
@@ -111,7 +114,7 @@ public:
|
||||
static std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> get_create_image_info(const std::vector<encoder_settings> &);
|
||||
#endif
|
||||
|
||||
video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, bool async_send);
|
||||
video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier, bool async_send);
|
||||
virtual ~video_encoder();
|
||||
|
||||
// return value: true if image should be transitioned to queue and layout for vulkan video encode
|
||||
@@ -121,12 +124,15 @@ public:
|
||||
|
||||
virtual void on_feedback(const from_headset::feedback &);
|
||||
virtual void reset();
|
||||
virtual void set_bitrate(int bitrate_bps);
|
||||
|
||||
void encode(wivrn_session & cnx,
|
||||
const to_headset::video_stream_data_shard::view_info_t & view_info,
|
||||
uint64_t frame_index);
|
||||
|
||||
protected:
|
||||
std::atomic_int pending_bitrate;
|
||||
|
||||
// called on present to submit command buffers for the image.
|
||||
virtual std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) = 0;
|
||||
// called after command buffer passed in present_image was submitted
|
||||
|
||||
@@ -139,7 +139,7 @@ video_encoder_nvenc::video_encoder_nvenc(
|
||||
encoder_settings & settings,
|
||||
float fps,
|
||||
uint8_t stream_idx) :
|
||||
video_encoder(stream_idx, settings.channels, true),
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true),
|
||||
vk(vk),
|
||||
fps(fps),
|
||||
bitrate(settings.bitrate)
|
||||
|
||||
@@ -59,7 +59,7 @@ wivrn::video_encoder_vulkan::video_encoder_vulkan(
|
||||
float fps,
|
||||
uint8_t stream_idx,
|
||||
const encoder_settings & settings) :
|
||||
video_encoder(stream_idx, settings.channels, true), vk(vk), encode_caps(patch_capabilities(in_encode_caps)), rect(rect), fps(fps)
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true), vk(vk), encode_caps(patch_capabilities(in_encode_caps)), rect(rect), fps(fps)
|
||||
{
|
||||
// Initialize Rate control
|
||||
U_LOG_D("Supported rate control modes: %s", vk::to_string(encode_caps.rateControlModes).c_str());
|
||||
@@ -693,6 +693,27 @@ std::pair<bool, vk::Semaphore> wivrn::video_encoder_vulkan::present_image(vk::Im
|
||||
session_initialized = true;
|
||||
}
|
||||
|
||||
if (auto bitrate = pending_bitrate.exchange(BITRATE_UNCHANGED); bitrate and rate_control)
|
||||
{
|
||||
rate_control_layer.averageBitrate = std::min<uint64_t>(bitrate, encode_caps.maxBitrate);
|
||||
switch (rate_control->rateControlMode)
|
||||
{
|
||||
case vk::VideoEncodeRateControlModeFlagBitsKHR::eCbr:
|
||||
rate_control_layer.maxBitrate = rate_control_layer.averageBitrate;
|
||||
break;
|
||||
case vk::VideoEncodeRateControlModeFlagBitsKHR::eVbr:
|
||||
rate_control_layer.maxBitrate = std::min(2 * rate_control_layer.averageBitrate, encode_caps.maxBitrate);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
video_cmd_buf.controlVideoCodingKHR(
|
||||
vk::VideoCodingControlInfoKHR{
|
||||
.pNext = &rate_control.value(),
|
||||
.flags = vk::VideoCodingControlFlagBitsKHR::eEncodeRateControl,
|
||||
});
|
||||
}
|
||||
|
||||
vk::VideoEncodeInfoKHR encode_info{
|
||||
.pNext = encode_info_next(frame_num, slot_index, ref_slot ? std::make_optional(ref_slot->info.slotIndex) : std::nullopt),
|
||||
.dstBuffer = slot_item.output_buffer,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "video_encoder_x264.h"
|
||||
|
||||
#include "encoder/video_encoder.h"
|
||||
#include "encoder_settings.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
@@ -90,7 +91,7 @@ video_encoder_x264::video_encoder_x264(
|
||||
encoder_settings & settings,
|
||||
float fps,
|
||||
uint8_t stream_idx) :
|
||||
video_encoder(stream_idx, settings.channels, false)
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, false)
|
||||
{
|
||||
if (settings.codec != h264)
|
||||
{
|
||||
@@ -230,6 +231,13 @@ std::pair<bool, vk::Semaphore> video_encoder_x264::present_image(vk::Image y_cbc
|
||||
|
||||
std::optional<video_encoder::data> video_encoder_x264::encode(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot)
|
||||
{
|
||||
if (auto bitrate = pending_bitrate.exchange(BITRATE_UNCHANGED))
|
||||
{
|
||||
U_LOG_E("Reconfigure X264 encoder");
|
||||
idr = true;
|
||||
param.rc.i_bitrate = bitrate / 1000;
|
||||
x264_encoder_reconfig(enc, ¶m);
|
||||
}
|
||||
int num_nal;
|
||||
x264_nal_t * nal;
|
||||
auto & pic = in[slot].pic;
|
||||
|
||||
Reference in New Issue
Block a user