Remove remaining copies of encoded video
This commit is contained in:
@@ -184,15 +184,15 @@ void shard_accumulator::try_submit_frame(uint16_t shard_idx)
|
||||
decoder->push_data(payload, frame_index, false);
|
||||
|
||||
auto feedback = current.feedback;
|
||||
if (not data_shards.back()->view_info)
|
||||
if (not data_shards.front()->view_info)
|
||||
{
|
||||
spdlog::warn("shard with end_of_frame flag has no view_info");
|
||||
spdlog::warn("first shard has no view_info");
|
||||
return;
|
||||
}
|
||||
feedback.received_pose = data_shards.back()->view_info->pose;
|
||||
feedback.received_pose = data_shards.front()->view_info->pose;
|
||||
|
||||
// Try to extract a frame
|
||||
decoder->frame_completed(feedback, *data_shards.back()->view_info);
|
||||
decoder->frame_completed(feedback, *data_shards.front()->view_info);
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
@@ -116,12 +116,11 @@ bool VideoEncoderFFMPEG::once = set_log_level();
|
||||
void VideoEncoderFFMPEG::Encode(int index, bool idr, std::chrono::steady_clock::time_point target_timestamp)
|
||||
{
|
||||
PushFrame(index, idr, target_timestamp);
|
||||
AVPacket * enc_pkt = av_packet_alloc();
|
||||
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt);
|
||||
av_packet_ptr enc_pkt(av_packet_alloc());
|
||||
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
|
||||
if (err == 0)
|
||||
{
|
||||
SendData({enc_pkt->data, enc_pkt->data + enc_pkt->size});
|
||||
av_packet_free(&enc_pkt);
|
||||
SendData(std::span<uint8_t>(enc_pkt->data, enc_pkt->size), true);
|
||||
}
|
||||
if (err == AVERROR(EAGAIN))
|
||||
{
|
||||
|
||||
@@ -105,6 +105,11 @@ void AvDeleter::operator()(AVFilterGraph * x)
|
||||
avfilter_graph_free(&x);
|
||||
}
|
||||
|
||||
void AvDeleter::operator()(AVPacket * x)
|
||||
{
|
||||
av_packet_free(&x);
|
||||
}
|
||||
|
||||
av_frame_ptr
|
||||
make_av_frame(AVFrame * frame)
|
||||
{
|
||||
|
||||
@@ -27,6 +27,7 @@ struct AVBufferRef;
|
||||
struct AVFilterGraph;
|
||||
struct AVFrame;
|
||||
struct AVCodecContext;
|
||||
struct AVPacket;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
@@ -52,12 +53,15 @@ struct AvDeleter
|
||||
operator()(AVCodecContext *);
|
||||
void
|
||||
operator()(AVFilterGraph *);
|
||||
void
|
||||
operator()(AVPacket *);
|
||||
};
|
||||
|
||||
using av_buffer_ptr = std::unique_ptr<AVBufferRef, AvDeleter>;
|
||||
using av_frame_ptr = std::unique_ptr<AVFrame, AvDeleter>;
|
||||
using av_codec_context_ptr = std::unique_ptr<AVCodecContext, AvDeleter>;
|
||||
using av_filter_graph_ptr = std::unique_ptr<AVFilterGraph, AvDeleter>;
|
||||
using av_packet_ptr = std::unique_ptr<AVPacket, AvDeleter>;
|
||||
|
||||
av_buffer_ptr
|
||||
make_av_buffer(AVBufferRef *);
|
||||
|
||||
@@ -93,67 +93,44 @@ void VideoEncoder::Encode(wivrn_session & cnx,
|
||||
// Prepare the video shard template
|
||||
shard.stream_item_idx = stream_idx;
|
||||
shard.frame_idx = frame_index;
|
||||
shard.shard_idx = -1;
|
||||
shard.view_info.reset();
|
||||
shard.shard_idx = 0;
|
||||
shard.view_info = view_info;
|
||||
|
||||
Encode(index, idr, target_timestamp);
|
||||
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx);
|
||||
if (shard.payload.empty())
|
||||
return;
|
||||
const size_t view_info_size = sizeof(to_headset::video_stream_data_shard::view_info_t);
|
||||
if (shard.payload.size() + view_info_size > to_headset::video_stream_data_shard::max_payload_size)
|
||||
{
|
||||
// Push empty data so previous shard is sent and counters are set
|
||||
PushShard({}, to_headset::video_stream_data_shard::start_of_slice | to_headset::video_stream_data_shard::end_of_slice);
|
||||
}
|
||||
shard.view_info = view_info;
|
||||
shard.flags |= to_headset::video_stream_data_shard::end_of_frame;
|
||||
cnx.send_stream(shard);
|
||||
|
||||
}
|
||||
|
||||
void VideoEncoder::SendData(std::vector<uint8_t> && data_)
|
||||
void VideoEncoder::SendData(std::span<uint8_t> data, bool end_of_frame)
|
||||
{
|
||||
auto & max_payload_size = to_headset::video_stream_data_shard::max_payload_size;
|
||||
std::lock_guard lock(mutex);
|
||||
data = std::move(data_);
|
||||
#if 0
|
||||
std::ofstream debug("/tmp/video_dump-" + std::to_string(stream_idx), std::ios::app);
|
||||
debug.write((char*)data.data(), data.size());
|
||||
#endif
|
||||
uint8_t flags = to_headset::video_stream_data_shard::start_of_slice;
|
||||
if (data.size() <= max_payload_size)
|
||||
{
|
||||
PushShard(data, flags | to_headset::video_stream_data_shard::end_of_slice);
|
||||
}
|
||||
if (shard.shard_idx == 0)
|
||||
cnx->dump_time("send_start", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
|
||||
|
||||
shard.flags = to_headset::video_stream_data_shard::start_of_slice;
|
||||
auto begin = data.begin();
|
||||
auto end = data.end();
|
||||
while (begin != end)
|
||||
{
|
||||
const size_t view_info_size = sizeof(to_headset::video_stream_data_shard::view_info_t);
|
||||
const size_t max_payload_size = to_headset::video_stream_data_shard::max_payload_size - (shard.view_info ? view_info_size : 0);
|
||||
auto next = std::min(end, begin + max_payload_size);
|
||||
if (next == end)
|
||||
{
|
||||
flags |= to_headset::video_stream_data_shard::end_of_slice;
|
||||
shard.flags |= to_headset::video_stream_data_shard::end_of_slice;
|
||||
if (end_of_frame)
|
||||
shard.flags |= to_headset::video_stream_data_shard::end_of_frame;
|
||||
}
|
||||
PushShard({begin, next}, flags);
|
||||
begin = next;
|
||||
flags = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void VideoEncoder::PushShard(const std::span<uint8_t> & payload, uint8_t flags)
|
||||
{
|
||||
if (shard.shard_idx != decltype(shard.shard_idx)(-1))
|
||||
{
|
||||
shard.payload = {begin, next};
|
||||
cnx->send_stream(shard);
|
||||
++shard.shard_idx;
|
||||
shard.flags = 0;
|
||||
shard.view_info.reset();
|
||||
begin = next;
|
||||
}
|
||||
else
|
||||
{
|
||||
cnx->dump_time("send_start", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
|
||||
}
|
||||
++shard.shard_idx;
|
||||
shard.flags = flags;
|
||||
shard.payload = payload;
|
||||
}
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include "encoder_settings.h"
|
||||
@@ -50,10 +49,8 @@ private:
|
||||
// temporary data
|
||||
wivrn_session * cnx;
|
||||
|
||||
// shard waiting to be sent
|
||||
// shard to send
|
||||
to_headset::video_stream_data_shard shard;
|
||||
// and its data
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
public:
|
||||
static std::unique_ptr<VideoEncoder> Create(vk_bundle * vk,
|
||||
@@ -90,10 +87,7 @@ protected:
|
||||
// encode the image at provided index.
|
||||
virtual void Encode(int index, bool idr, std::chrono::steady_clock::time_point target_timestamp) = 0;
|
||||
|
||||
void SendData(std::vector<uint8_t> && data);
|
||||
|
||||
private:
|
||||
void PushShard(const std::span<uint8_t>& payload, uint8_t flags);
|
||||
void SendData(std::span<uint8_t> data, bool end_of_frame);
|
||||
};
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
@@ -282,7 +282,7 @@ void VideoEncoderNvenc::Encode(int index, bool idr, std::chrono::steady_clock::t
|
||||
NVENC_CHECK(fn.nvEncLockBitstream(session_handle, ¶m2));
|
||||
|
||||
SendData(
|
||||
{(uint8_t *)param2.bitstreamBufferPtr, (uint8_t *)param2.bitstreamBufferPtr + param2.bitstreamSizeInBytes});
|
||||
{(uint8_t *)param2.bitstreamBufferPtr, (uint8_t *)param2.bitstreamBufferPtr + param2.bitstreamSizeInBytes}, true);
|
||||
|
||||
NVENC_CHECK(fn.nvEncUnlockBitstream(session_handle, bitstreamBuffer));
|
||||
|
||||
|
||||
@@ -33,9 +33,10 @@ void VideoEncoderX264::ProcessCb(x264_t * h, x264_nal_t * nal, void * opaque)
|
||||
switch (nal->i_type)
|
||||
{
|
||||
case NAL_SPS:
|
||||
case NAL_PPS:
|
||||
self->SendData(std::move(data));
|
||||
case NAL_PPS: {
|
||||
self->SendData(data, false);
|
||||
break;
|
||||
}
|
||||
case NAL_SLICE:
|
||||
case NAL_SLICE_DPA:
|
||||
case NAL_SLICE_DPB:
|
||||
@@ -51,7 +52,7 @@ void VideoEncoderX264::ProcessNal(pending_nal && nal)
|
||||
if (nal.first_mb == next_mb)
|
||||
{
|
||||
next_mb = nal.last_mb + 1;
|
||||
SendData(std::move(nal.data));
|
||||
SendData(nal.data, next_mb == num_mb);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -59,8 +60,8 @@ void VideoEncoderX264::ProcessNal(pending_nal && nal)
|
||||
}
|
||||
while ((not pending_nals.empty()) and pending_nals.front().first_mb == next_mb)
|
||||
{
|
||||
SendData(std::move(pending_nals.front().data));
|
||||
next_mb = pending_nals.front().last_mb + 1;
|
||||
SendData(pending_nals.front().data, next_mb == num_mb);
|
||||
pending_nals.pop_front();
|
||||
}
|
||||
}
|
||||
@@ -99,6 +100,9 @@ VideoEncoderX264::VideoEncoderX264(
|
||||
settings.video_width += settings.video_width % 2;
|
||||
settings.video_height += settings.video_height % 2;
|
||||
|
||||
num_mb = ((settings.video_width + 15) / 16)
|
||||
* ((settings.video_height + 15) / 16);
|
||||
|
||||
converter = std::make_unique<YuvConverter>(
|
||||
vk,
|
||||
pool,
|
||||
@@ -171,6 +175,10 @@ void VideoEncoderX264::Encode(int index, bool idr, std::chrono::steady_clock::ti
|
||||
next_mb = 0;
|
||||
assert(pending_nals.empty());
|
||||
int size = x264_encoder_encode(enc, &nal, &num_nal, &pic_in, &pic_out);
|
||||
if (next_mb != num_mb)
|
||||
{
|
||||
U_LOG_W("unexpected macroblock count: %d", next_mb);
|
||||
}
|
||||
if (size < 0)
|
||||
{
|
||||
U_LOG_W("x264_encoder_encode failed: %d", size);
|
||||
|
||||
@@ -48,6 +48,7 @@ class VideoEncoderX264 : public VideoEncoder
|
||||
|
||||
std::mutex mutex;
|
||||
int next_mb;
|
||||
int num_mb; // Number of macroblocks in a frame
|
||||
std::list<pending_nal> pending_nals;
|
||||
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user