Only send I frames when necessary

This commit is contained in:
Patrick Nicolas
2023-12-16 14:16:46 +01:00
committed by Guillaume Meunier
parent 6e14bac589
commit df786362e7
10 changed files with 42 additions and 15 deletions
-1
View File
@@ -171,7 +171,6 @@ struct feedback
// Timestamps
uint64_t received_first_packet;
uint64_t received_last_packet;
uint64_t reconstructed;
uint64_t sent_to_decoder;
uint64_t received_from_decoder;
uint64_t blitted;
+3 -1
View File
@@ -23,6 +23,7 @@
#include <cassert>
#include <chrono>
#include <filesystem>
#include <limits>
#include <optional>
#include <stdexcept>
@@ -168,6 +169,7 @@ VideoEncoderVA::VideoEncoderVA(vk_bundle * vk, const xrt::drivers::wivrn::encode
encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI;
encoder_ctx->max_b_frames = 0;
encoder_ctx->bit_rate = settings.bitrate;
encoder_ctx->gop_size = std::numeric_limits<decltype(encoder_ctx->gop_size)>::max();
set_hwframe_ctx(encoder_ctx.get(), hw_ctx_vaapi.get());
@@ -199,7 +201,7 @@ void VideoEncoderVA::PushFrame(uint32_t frame_index, bool idr, std::chrono::stea
throw std::system_error(err, av_error_category(), "av_buffersink_get_frame failed");
}
encoder_frame->pict_type = idr ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_NONE;
encoder_frame->pict_type = idr ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
encoder_frame->pts = pts.time_since_epoch().count();
err = avcodec_send_frame(encoder_ctx.get(), encoder_frame.get());
if (err)
+7 -3
View File
@@ -80,11 +80,15 @@ std::unique_ptr<VideoEncoder> VideoEncoder::Create(
return res;
}
void VideoEncoder::SyncNeeded()
{
sync_needed = true;
}
void VideoEncoder::Encode(wivrn_session & cnx,
const to_headset::video_stream_data_shard::view_info_t & view_info,
uint64_t frame_index,
int index,
bool idr)
int index)
{
this->cnx = &cnx;
auto target_timestamp = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(view_info.display_time));
@@ -96,7 +100,7 @@ void VideoEncoder::Encode(wivrn_session & cnx,
shard.shard_idx = 0;
shard.view_info = view_info;
Encode(index, idr, target_timestamp);
Encode(index, sync_needed.exchange(false), target_timestamp);
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx);
}
+7 -2
View File
@@ -21,6 +21,7 @@
#include "vk/vk_cmd_pool.h"
#include "vk/vk_helpers.h"
#include <atomic>
#include <chrono>
#include <memory>
#include <mutex>
@@ -52,6 +53,8 @@ private:
// shard to send
to_headset::video_stream_data_shard shard;
std::atomic_bool sync_needed = true;
public:
static std::unique_ptr<VideoEncoder> Create(vk_bundle * vk,
vk_cmd_pool & pool,
@@ -77,11 +80,13 @@ public:
virtual void PresentImage(int index, VkCommandBuffer * out_buffer)
{}
// The other end lost a frame and needs to resynchronize
void SyncNeeded();
void Encode(wivrn_session & cnx,
const to_headset::video_stream_data_shard::view_info_t & view_info,
uint64_t frame_index,
int index,
bool idr);
int index);
protected:
// encode the image at provided index.
+1 -1
View File
@@ -266,7 +266,7 @@ void VideoEncoderNvenc::Encode(int index, bool idr, std::chrono::steady_clock::t
param.inputWidth = width;
param.inputHeight = height;
param.inputPitch = width;
param.encodePicFlags = idr ? (uint32_t)NV_ENC_PIC_TYPE_IDR : 0;
param.encodePicFlags = (uint32_t)(idr ? NV_ENC_PIC_TYPE_IDR : NV_ENC_PIC_TYPE_P);
param.frameIdx = 0;
param.inputTimeStamp = 0;
param.inputBuffer = param4.mappedResource;
+2 -1
View File
@@ -123,6 +123,7 @@ VideoEncoderX264::VideoEncoderX264(
param.i_fps_den = 1'000'000;
param.b_repeat_headers = 1;
param.b_aud = 0;
param.i_keyint_max = X264_KEYINT_MAX_INFINITE;
// colour definitions, actually ignored by decoder
param.vui.b_fullrange = 1;
@@ -170,7 +171,7 @@ void VideoEncoderX264::Encode(int index, bool idr, std::chrono::steady_clock::ti
{
int num_nal;
x264_nal_t * nal;
pic_in.i_type = idr ? X264_TYPE_IDR : X264_TYPE_AUTO;
pic_in.i_type = idr ? X264_TYPE_IDR : X264_TYPE_P;
pic_in.i_pts = pts.time_since_epoch().count();
next_mb = 0;
assert(pending_nals.empty());
+11 -3
View File
@@ -503,9 +503,7 @@ static void * comp_wivrn_present_thread(void * void_param)
{
for (auto & encoder: param->encoders)
{
bool idr_requested = false;
encoder->Encode(*cn->cnx, psc_image.view_info, psc_image.frame_index, presenting_index, idr_requested);
encoder->Encode(*cn->cnx, psc_image.view_info, psc_image.frame_index, presenting_index);
}
}
catch (...)
@@ -698,6 +696,16 @@ static void comp_wivrn_info_gpu(struct comp_target * ct, int64_t frame_id, uint6
u_pc_info_gpu(cn->upc, frame_id, gpu_start_ns, gpu_end_ns, when_ns);
}
void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, const clock_offset & o)
{
if (not feedback.sent_to_decoder)
{
if (encoders.size() < feedback.stream_index)
return;
encoders[feedback.stream_index]->SyncNeeded();
}
}
wivrn_comp_target::wivrn_comp_target(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx, struct comp_compositor * c, float fps) :
comp_target{},
cnx(cnx)
+3
View File
@@ -19,6 +19,7 @@
#pragma once
#include "wivrn_packets.h"
#include "wivrn_session.h"
#include "main/comp_target.h"
@@ -90,6 +91,8 @@ struct wivrn_comp_target : public comp_target
std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx;
wivrn_comp_target(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx, struct comp_compositor * c, float fps);
void on_feedback(const from_headset::feedback &, const clock_offset &);
};
} // namespace xrt::drivers::wivrn
+4 -3
View File
@@ -59,7 +59,8 @@ struct wivrn_comp_target_factory : public comp_target_factory
static bool create_target(const struct comp_target_factory * ctf, struct comp_compositor * c, struct comp_target ** out_ct)
{
auto self = (wivrn_comp_target_factory *)ctf;
*out_ct = new wivrn_comp_target(self->session, c, self->fps);
self->session->comp_target = new wivrn_comp_target(self->session, c, self->fps);;
*out_ct = self->session->comp_target;
return true;
}
};
@@ -198,18 +199,18 @@ void wivrn_session::operator()(from_headset::timesync_response && timesync)
void wivrn_session::operator()(from_headset::feedback && feedback)
{
assert(comp_target);
clock_offset o;
{
std::lock_guard lock(mutex);
o = offset;
}
comp_target->on_feedback(feedback, o);
if (feedback.received_first_packet)
dump_time("receive_start", feedback.frame_index, o.from_headset(feedback.received_first_packet), feedback.stream_index);
if (feedback.received_last_packet)
dump_time("receive_end", feedback.frame_index, o.from_headset(feedback.received_last_packet), feedback.stream_index);
if (feedback.reconstructed)
dump_time("reconstructed", feedback.frame_index, o.from_headset(feedback.reconstructed), feedback.stream_index);
if (feedback.sent_to_decoder)
dump_time("decode_start", feedback.frame_index, o.from_headset(feedback.sent_to_decoder), feedback.stream_index);
if (feedback.received_from_decoder)
+4
View File
@@ -37,12 +37,15 @@ struct audio_device;
struct xrt_system_devices;
struct xrt_space_overseer;
struct xrt_system_compositor;
struct wivrn_comp_target_factory;
namespace xrt::drivers::wivrn
{
struct wivrn_comp_target;
class wivrn_session : public std::enable_shared_from_this<wivrn_session>
{
friend wivrn_comp_target_factory;
wivrn_connection connection;
std::atomic<bool> quit = false;
@@ -52,6 +55,7 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
std::unique_ptr<wivrn_hmd> hmd;
std::unique_ptr<wivrn_controller> left_hand;
std::unique_ptr<wivrn_controller> right_hand;
wivrn_comp_target * comp_target;
clock_offset offset;
offset_estimator offset_est;