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