Sunshine/sunshine/video.cpp

1031 lines
27 KiB
C++
Raw Normal View History

//
// Created by loki on 6/6/19.
//
2020-02-08 16:26:38 +01:00
#include <atomic>
#include <thread>
#include <bitset>
extern "C" {
#include <libswscale/swscale.h>
2020-04-06 23:15:03 +03:00
#include <libavutil/hwcontext_d3d11va.h>
}
#include "platform/common.h"
#include "round_robin.h"
#include "sync.h"
#include "config.h"
#include "video.h"
#include "main.h"
namespace video {
using namespace std::literals;
void free_ctx(AVCodecContext *ctx) {
avcodec_free_context(&ctx);
}
void free_frame(AVFrame *frame) {
av_frame_free(&frame);
}
2020-03-25 10:51:32 +01:00
void free_buffer(AVBufferRef *ref) {
av_buffer_unref(&ref);
}
void free_packet(AVPacket *packet) {
av_packet_free(&packet);
}
using ctx_t = util::safe_ptr<AVCodecContext, free_ctx>;
using frame_t = util::safe_ptr<AVFrame, free_frame>;
2020-03-25 10:51:32 +01:00
using buffer_t = util::safe_ptr<AVBufferRef, free_buffer>;
using sws_t = util::safe_ptr<SwsContext, sws_freeContext>;
2020-02-08 16:26:38 +01:00
using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
2020-04-02 20:13:44 +02:00
void sw_img_to_frame(sws_t &sws, const platf::img_t &img, frame_t &frame);
2020-04-06 23:15:03 +03:00
2020-04-02 20:13:44 +02:00
void nv_d3d_img_to_frame(sws_t &sws, const platf::img_t &img, frame_t &frame);
2020-04-06 23:15:03 +03:00
util::Either<buffer_t, int> nv_d3d_make_hwdevice_ctx(platf::hwdevice_ctx_t *hwdevice_ctx);
2020-03-25 10:51:32 +01:00
struct encoder_t {
enum flag_e {
PASSED, // Is supported
REF_FRAMES_RESTRICT, // Set maximum reference frames
REF_FRAMES_AUTOSELECT, // Allow encoder to select maximum reference frames (If !REF_FRAMES_RESTRICT --> REF_FRAMES_AUTOSELECT)
MAX_FLAGS
};
2020-03-25 10:51:32 +01:00
struct option_t {
std::string name;
std::variant<int, int*, std::string, std::string*> value;
2020-03-25 10:51:32 +01:00
};
struct {
int h264_high;
int hevc_main;
int hevc_main_10;
} profile;
AVHWDeviceType dev_type;
2020-04-06 23:15:03 +03:00
AVPixelFormat dev_pix_fmt;
2020-03-25 10:51:32 +01:00
2020-04-06 23:15:03 +03:00
AVPixelFormat static_pix_fmt;
AVPixelFormat dynamic_pix_fmt;
2020-03-25 10:51:32 +01:00
struct {
std::vector<option_t> options;
std::string name;
std::bitset<MAX_FLAGS> capabilities;
2020-04-07 14:54:56 +03:00
bool operator[](flag_e flag) const {
return capabilities[(std::size_t)flag];
}
std::bitset<MAX_FLAGS>::reference operator[](flag_e flag) {
return capabilities[(std::size_t)flag];
}
2020-03-25 10:51:32 +01:00
} hevc, h264;
bool system_memory;
2020-04-02 20:13:44 +02:00
std::function<void(sws_t &, const platf::img_t&, frame_t&)> img_to_frame;
2020-04-06 23:15:03 +03:00
std::function<util::Either<buffer_t, int>(platf::hwdevice_ctx_t *hwdevice)> make_hwdevice_ctx;
2020-03-25 10:51:32 +01:00
};
struct session_t {
buffer_t hwdevice;
ctx_t ctx;
frame_t frame;
AVPixelFormat sw_format;
2020-03-25 10:51:32 +01:00
int sws_color_format;
};
static encoder_t nvenc {
{ 2, 0, 1 },
AV_HWDEVICE_TYPE_D3D11VA,
AV_PIX_FMT_D3D11,
2020-04-06 23:15:03 +03:00
AV_PIX_FMT_NV12, AV_PIX_FMT_NV12,
2020-03-25 10:51:32 +01:00
{
2020-04-08 02:15:08 +03:00
{ {"forced-idr"s, 1} }, "hevc_nvenc"s
2020-03-25 10:51:32 +01:00
},
{
2020-04-10 15:39:50 +03:00
{
{ "forced-idr"s, 1},
{ "profile"s, "high"s },
{ "preset"s , "llhp" },
{ "rc"s, "cbr_ld_hq"s },
}, "h264_nvenc"s
2020-03-25 10:51:32 +01:00
},
false,
2020-04-06 23:15:03 +03:00
nv_d3d_img_to_frame,
nv_d3d_make_hwdevice_ctx
2020-03-25 10:51:32 +01:00
};
static encoder_t software {
{ FF_PROFILE_H264_HIGH, FF_PROFILE_HEVC_MAIN, FF_PROFILE_HEVC_MAIN_10 },
AV_HWDEVICE_TYPE_NONE,
AV_PIX_FMT_NONE,
2020-04-06 23:15:03 +03:00
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10,
2020-03-25 10:51:32 +01:00
{
// x265's Info SEI is so long that it causes the IDR picture data to be
// kicked to the 2nd packet in the frame, breaking Moonlight's parsing logic.
// It also looks like gop_size isn't passed on to x265, so we have to set
// 'keyint=-1' in the parameters ourselves.
{
{ "x265-params"s, "info=0:keyint=-1"s },
{ "preset"s, &config::video.preset },
{ "tune"s, &config::video.tune }
}, "libx265"s
2020-03-25 10:51:32 +01:00
},
{
{
{ "preset"s, &config::video.preset },
{ "tune"s, &config::video.tune }
}, "libx264"s
2020-03-25 10:51:32 +01:00
},
true,
2020-04-06 23:15:03 +03:00
sw_img_to_frame,
nullptr
2020-03-25 10:51:32 +01:00
};
static std::vector<encoder_t> encoders {
nvenc, software
};
2020-02-08 16:26:38 +01:00
struct capture_ctx_t {
img_event_t images;
std::chrono::nanoseconds delay;
};
struct capture_thread_ctx_t {
std::shared_ptr<safe::queue_t<capture_ctx_t>> capture_ctx_queue;
std::thread capture_thread;
safe::signal_t reinit_event;
const encoder_t *encoder_p;
util::sync_t<std::weak_ptr<platf::display_t>> display_wp;
2020-02-08 16:26:38 +01:00
};
void reset_display(std::shared_ptr<platf::display_t> &disp, AVHWDeviceType type) {
// We try this twice, in case we still get an error on reinitialization
for(int x = 0; x < 2; ++x) {
disp.reset();
disp = platf::display(type);
if(disp) {
break;
}
std::this_thread::sleep_for(200ms);
}
}
void captureThread(
std::shared_ptr<safe::queue_t<capture_ctx_t>> capture_ctx_queue,
util::sync_t<std::weak_ptr<platf::display_t>> &display_wp,
safe::signal_t &reinit_event,
const encoder_t &encoder
) {
2020-02-08 16:26:38 +01:00
std::vector<capture_ctx_t> capture_ctxs;
auto fg = util::fail_guard([&]() {
capture_ctx_queue->stop();
// Stop all sessions listening to this thread
for(auto &capture_ctx : capture_ctxs) {
capture_ctx.images->stop();
}
for(auto &capture_ctx : capture_ctx_queue->unsafe()) {
capture_ctx.images->stop();
}
});
std::chrono::nanoseconds delay = 1s;
auto disp = platf::display(encoder.dev_type);
if(!disp) {
return;
}
display_wp = disp;
std::vector<std::shared_ptr<platf::img_t>> imgs(12);
2020-04-10 15:39:50 +03:00
auto round_robin = util::make_round_robin<std::shared_ptr<platf::img_t>>(std::begin(imgs), std::end(imgs));
for(auto &img : imgs) {
img = disp->alloc_img();
if(!img) {
BOOST_LOG(error) << "Couldn't initialize an image"sv;
return;
}
}
2020-04-10 15:39:50 +03:00
if(auto capture_ctx = capture_ctx_queue->pop()) {
capture_ctxs.emplace_back(std::move(*capture_ctx));
delay = capture_ctxs.back().delay;
2020-04-06 23:15:03 +03:00
}
auto next_frame = std::chrono::steady_clock::now();
2020-02-08 16:26:38 +01:00
while(capture_ctx_queue->running()) {
while(capture_ctx_queue->peek()) {
capture_ctxs.emplace_back(std::move(*capture_ctx_queue->pop()));
delay = std::min(delay, capture_ctxs.back().delay);
2020-02-08 16:26:38 +01:00
}
auto now = std::chrono::steady_clock::now();
auto &img = *round_robin++;
2020-04-07 18:57:59 +03:00
while(img.use_count() > 1) {}
2020-04-08 02:15:08 +03:00
2020-04-10 15:39:50 +03:00
auto status = disp->snapshot(img.get(), 1000ms, display_cursor);
switch (status) {
case platf::capture_e::reinit: {
reinit_event.raise(true);
// Some classes of images contain references to the display --> display won't delete unless img is deleted
for(auto &img : imgs) {
img.reset();
}
// Some classes of display cannot have multiple instances at once
disp.reset();
// display_wp is modified in this thread only
while(!display_wp->expired()) {
std::this_thread::sleep_for(100ms);
}
reset_display(disp, encoder.dev_type);
if(!disp) {
return;
}
display_wp = disp;
// Re-allocate images
for(auto &img : imgs) {
img = disp->alloc_img();
if(!img) {
BOOST_LOG(error) << "Couldn't initialize an image"sv;
return;
}
}
reinit_event.reset();
continue;
}
case platf::capture_e::error:
2020-04-10 15:39:50 +03:00
return;
case platf::capture_e::timeout:
2020-04-10 15:39:50 +03:00
std::this_thread::sleep_for(1ms);
continue;
case platf::capture_e::ok:
break;
default:
BOOST_LOG(error) << "Unrecognized capture status ["sv << (int)status << ']';
return;
2020-02-09 17:30:11 +01:00
}
2020-02-08 16:26:38 +01:00
KITTY_WHILE_LOOP(auto capture_ctx = std::begin(capture_ctxs), capture_ctx != std::end(capture_ctxs), {
2020-02-08 16:26:38 +01:00
if(!capture_ctx->images->running()) {
auto tmp_delay = capture_ctx->delay;
2020-02-08 16:26:38 +01:00
capture_ctx = capture_ctxs.erase(capture_ctx);
if(tmp_delay == delay) {
delay = std::min_element(std::begin(capture_ctxs), std::end(capture_ctxs), [](const auto &l, const auto &r) {
return l.delay < r.delay;
})->delay;
}
continue;
2020-02-08 16:26:38 +01:00
}
2020-02-09 17:30:11 +01:00
capture_ctx->images->raise(img);
2020-02-09 17:30:11 +01:00
++capture_ctx;
})
2020-04-10 15:39:50 +03:00
if(next_frame > now) {
std::this_thread::sleep_until(next_frame);
}
next_frame += delay;
2020-02-08 16:26:38 +01:00
}
}
2020-04-10 15:39:50 +03:00
int start_capture(capture_thread_ctx_t &capture_thread_ctx) {
capture_thread_ctx.encoder_p = &encoders.front();
capture_thread_ctx.reinit_event.reset();
capture_thread_ctx.capture_ctx_queue = std::make_shared<safe::queue_t<capture_ctx_t>>();
capture_thread_ctx.capture_thread = std::thread {
captureThread,
capture_thread_ctx.capture_ctx_queue,
std::ref(capture_thread_ctx.display_wp),
std::ref(capture_thread_ctx.reinit_event),
std::ref(*capture_thread_ctx.encoder_p)
};
return 0;
}
void end_capture(capture_thread_ctx_t &capture_thread_ctx) {
capture_thread_ctx.capture_ctx_queue->stop();
capture_thread_ctx.capture_thread.join();
}
2020-04-06 23:15:03 +03:00
util::Either<buffer_t, int> hwdevice_ctx(AVHWDeviceType type, void *hwdevice_ctx) {
2020-03-25 10:51:32 +01:00
buffer_t ctx;
2020-04-06 23:15:03 +03:00
int err;
if(hwdevice_ctx) {
ctx.reset(av_hwdevice_ctx_alloc(type));
((AVHWDeviceContext*)ctx.get())->hwctx = hwdevice_ctx;
err = av_hwdevice_ctx_init(ctx.get());
}
else {
AVBufferRef *ref {};
err = av_hwdevice_ctx_create(&ref, type, nullptr, nullptr, 0);
ctx.reset(ref);
}
2020-03-25 10:51:32 +01:00
if(err < 0) {
return err;
}
return ctx;
}
int hwframe_ctx(ctx_t &ctx, buffer_t &hwdevice, AVPixelFormat format) {
buffer_t frame_ref { av_hwframe_ctx_alloc(hwdevice.get())};
auto frame_ctx = (AVHWFramesContext*)frame_ref->data;
frame_ctx->format = ctx->pix_fmt;
frame_ctx->sw_format = format;
frame_ctx->height = ctx->height;
frame_ctx->width = ctx->width;
2020-04-06 23:15:03 +03:00
frame_ctx->initial_pool_size = 0;
2020-03-25 10:51:32 +01:00
if(auto err = av_hwframe_ctx_init(frame_ref.get()); err < 0) {
return err;
}
ctx->hw_frames_ctx = av_buffer_ref(frame_ref.get());
return 0;
}
int encode(int64_t frame_nr, ctx_t &ctx, frame_t &frame, packet_queue_t &packets, void *channel_data) {
2020-03-25 10:51:32 +01:00
frame->pts = frame_nr;
/* send the frame to the encoder */
2020-03-25 10:51:32 +01:00
auto ret = avcodec_send_frame(ctx.get(), frame.get());
if (ret < 0) {
2020-04-06 23:15:03 +03:00
char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
BOOST_LOG(error) << "Could not send a frame for encoding: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, ret);
return -1;
}
while (ret >= 0) {
2020-02-08 16:26:38 +01:00
auto packet = std::make_unique<packet_t::element_type>(nullptr);
ret = avcodec_receive_packet(ctx.get(), packet.get());
2020-01-18 11:33:16 +01:00
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
2020-01-18 11:33:16 +01:00
}
else if (ret < 0) {
return ret;
}
2020-02-08 16:26:38 +01:00
packet->channel_data = channel_data;
2019-12-11 19:06:52 +01:00
packets->raise(std::move(packet));
}
return 0;
}
2020-04-06 23:15:03 +03:00
std::optional<session_t> make_session(const encoder_t &encoder, const config_t &config, platf::hwdevice_ctx_t *device_ctx) {
bool hardware = encoder.dev_type != AV_HWDEVICE_TYPE_NONE;
2020-03-25 10:51:32 +01:00
auto &video_format = config.videoFormat == 0 ? encoder.h264 : encoder.hevc;
2020-04-07 14:54:56 +03:00
assert(video_format[encoder_t::PASSED]);
2020-02-08 16:26:38 +01:00
2020-03-25 10:51:32 +01:00
auto codec = avcodec_find_encoder_by_name(video_format.name.c_str());
if(!codec) {
BOOST_LOG(error) << "Couldn't open ["sv << video_format.name << ']';
2020-02-08 16:26:38 +01:00
2020-03-25 10:51:32 +01:00
return std::nullopt;
2020-02-08 16:26:38 +01:00
}
2020-03-25 10:51:32 +01:00
ctx_t ctx {avcodec_alloc_context3(codec) };
ctx->width = config.width;
ctx->height = config.height;
2020-03-25 10:51:32 +01:00
ctx->time_base = AVRational{1, config.framerate};
ctx->framerate = AVRational{config.framerate, 1};
if(config.videoFormat == 0) {
2020-03-25 10:51:32 +01:00
ctx->profile = encoder.profile.h264_high;
}
else if(config.dynamicRange == 0) {
2020-03-25 10:51:32 +01:00
ctx->profile = encoder.profile.hevc_main;
}
else {
2020-03-25 10:51:32 +01:00
ctx->profile = encoder.profile.hevc_main_10;
}
2020-03-25 10:51:32 +01:00
// B-frames delay decoder output, so never use them
ctx->max_b_frames = 0;
// Use an infinite GOP length since I-frames are generated on demand
ctx->gop_size = std::numeric_limits<int>::max();
ctx->keyint_min = ctx->gop_size;
2020-04-07 14:54:56 +03:00
if(config.numRefFrames == 0) {
ctx->refs = video_format[encoder_t::REF_FRAMES_AUTOSELECT] ? 0 : 1;
}
else {
// Some client decoders have limits on the number of reference frames
ctx->refs = video_format[encoder_t::REF_FRAMES_RESTRICT] ? config.numRefFrames : 0;
}
2020-03-25 10:51:32 +01:00
ctx->flags |= (AV_CODEC_FLAG_CLOSED_GOP | AV_CODEC_FLAG_LOW_DELAY);
ctx->flags2 |= AV_CODEC_FLAG2_FAST;
ctx->color_range = (config.encoderCscMode & 0x1) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2020-03-25 10:51:32 +01:00
int sws_color_space;
switch (config.encoderCscMode >> 1) {
case 0:
default:
// Rec. 601
ctx->color_primaries = AVCOL_PRI_SMPTE170M;
ctx->color_trc = AVCOL_TRC_SMPTE170M;
ctx->colorspace = AVCOL_SPC_SMPTE170M;
2020-03-25 10:51:32 +01:00
sws_color_space = SWS_CS_SMPTE170M;
break;
case 1:
// Rec. 709
ctx->color_primaries = AVCOL_PRI_BT709;
ctx->color_trc = AVCOL_TRC_BT709;
ctx->colorspace = AVCOL_SPC_BT709;
2020-03-25 10:51:32 +01:00
sws_color_space = SWS_CS_ITU709;
break;
case 2:
// Rec. 2020
ctx->color_primaries = AVCOL_PRI_BT2020;
ctx->color_trc = AVCOL_TRC_BT2020_10;
ctx->colorspace = AVCOL_SPC_BT2020_NCL;
2020-03-25 10:51:32 +01:00
sws_color_space = SWS_CS_BT2020;
break;
}
AVPixelFormat sw_fmt;
2020-03-25 10:51:32 +01:00
if(config.dynamicRange == 0) {
2020-04-06 23:15:03 +03:00
sw_fmt = encoder.static_pix_fmt;
2020-03-25 10:51:32 +01:00
}
else {
2020-04-06 23:15:03 +03:00
sw_fmt = encoder.dynamic_pix_fmt;
2020-03-25 10:51:32 +01:00
}
2020-04-06 23:15:03 +03:00
buffer_t hwdevice;
2020-03-25 10:51:32 +01:00
if(hardware) {
2020-04-06 23:15:03 +03:00
ctx->pix_fmt = encoder.dev_pix_fmt;
2020-03-25 10:51:32 +01:00
2020-04-06 23:15:03 +03:00
auto buf_or_error = encoder.make_hwdevice_ctx(device_ctx);
if(buf_or_error.has_right()) {
return std::nullopt;
}
2020-04-06 23:15:03 +03:00
hwdevice = std::move(buf_or_error.left());
if(hwframe_ctx(ctx, hwdevice, sw_fmt)) {
2020-03-25 10:51:32 +01:00
return std::nullopt;
}
2020-04-10 15:39:50 +03:00
ctx->slices = config.slicesPerFrame;
2020-03-25 10:51:32 +01:00
}
else /* software */ {
ctx->pix_fmt = sw_fmt;
2020-03-25 10:51:32 +01:00
// Clients will request for the fewest slices per frame to get the
// most efficient encode, but we may want to provide more slices than
// requested to ensure we have enough parallelism for good performance.
ctx->slices = std::max(config.slicesPerFrame, config::video.min_threads);
ctx->thread_type = FF_THREAD_SLICE;
ctx->thread_count = ctx->slices;
}
AVDictionary *options {nullptr};
2020-03-25 10:51:32 +01:00
for(auto &option : video_format.options) {
std::visit(util::overloaded {
[&](int v) { av_dict_set_int(&options, option.name.c_str(), v, 0); },
[&](int *v) { av_dict_set_int(&options, option.name.c_str(), *v, 0); },
[&](const std::string &v) { av_dict_set(&options, option.name.c_str(), v.c_str(), 0); },
[&](std::string *v) { av_dict_set(&options, option.name.c_str(), v->c_str(), 0); }
}, option.value);
2020-03-25 10:51:32 +01:00
}
2019-12-21 16:41:51 +01:00
if(config.bitrate > 500) {
2020-03-25 10:51:32 +01:00
auto bitrate = config.bitrate * 1000;
ctx->rc_max_rate = bitrate;
2020-04-10 15:39:50 +03:00
ctx->rc_buffer_size = bitrate / config.framerate;
2020-03-25 10:51:32 +01:00
ctx->bit_rate = bitrate;
ctx->rc_min_rate = bitrate;
2019-12-21 16:41:51 +01:00
}
else if(config::video.crf != 0) {
2019-12-12 13:13:10 +01:00
av_dict_set_int(&options, "crf", config::video.crf, 0);
}
else {
av_dict_set_int(&options, "qp", config::video.qp, 0);
}
2020-04-08 02:15:08 +03:00
avcodec_open2(ctx.get(), codec, &options);
2020-03-25 10:51:32 +01:00
frame_t frame {av_frame_alloc() };
frame->format = ctx->pix_fmt;
frame->width = ctx->width;
frame->height = ctx->height;
if(hardware) {
2020-04-06 23:15:03 +03:00
frame->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx);
2020-03-25 10:51:32 +01:00
}
2020-04-06 23:15:03 +03:00
else /* software */ {
2020-03-25 10:51:32 +01:00
av_frame_get_buffer(frame.get(), 0);
}
return std::make_optional(session_t {
std::move(hwdevice),
std::move(ctx),
std::move(frame),
sw_fmt,
2020-03-25 10:51:32 +01:00
sws_color_space
});
}
void encode_run(
int &frame_nr, int &key_frame_nr, // Store progress of the frame number
safe::signal_t* shutdown_event, // Signal for shutdown event of the session
2020-03-25 10:51:32 +01:00
packet_queue_t packets,
idr_event_t idr_events,
img_event_t images,
2020-03-25 10:51:32 +01:00
config_t config,
2020-04-02 20:13:44 +02:00
platf::hwdevice_ctx_t *hwdevice_ctx,
safe::signal_t &reinit_event,
const encoder_t &encoder,
2020-03-25 10:51:32 +01:00
void *channel_data) {
2020-04-06 23:15:03 +03:00
auto session = make_session(encoder, config, hwdevice_ctx);
2020-03-25 10:51:32 +01:00
if(!session) {
return;
}
2020-04-10 15:39:50 +03:00
hwdevice_ctx->set_colorspace(session->sws_color_format, session->ctx->color_range);
auto delay = std::chrono::floor<std::chrono::nanoseconds>(1s) / config.framerate;
auto img_width = 0;
auto img_height = 0;
// Initiate scaling context with correct height and width
sws_t sws;
auto next_frame = std::chrono::steady_clock::now();
while(true) {
if(shutdown_event->peek() || reinit_event.peek() || !images->running()) {
2020-02-08 16:26:38 +01:00
break;
}
if(idr_events->peek()) {
2020-03-25 10:51:32 +01:00
session->frame->pict_type = AV_PICTURE_TYPE_I;
auto event = idr_events->pop();
TUPLE_2D_REF(_, end, *event);
2020-03-25 10:51:32 +01:00
frame_nr = end;
key_frame_nr = end + config.framerate;
}
2020-03-25 10:51:32 +01:00
else if(frame_nr == key_frame_nr) {
session->frame->pict_type = AV_PICTURE_TYPE_I;
}
std::this_thread::sleep_until(next_frame);
next_frame += delay;
// When Moonlight request an IDR frame, send frames even if there is no new captured frame
2020-03-25 10:51:32 +01:00
if(frame_nr > (key_frame_nr + config.framerate) || images->peek()) {
if(auto img = images->pop(delay)) {
2020-04-07 18:57:59 +03:00
const platf::img_t *img_p;
if(encoder.system_memory) {
auto new_width = img->width;
auto new_height = img->height;
if(img_width != new_width || img_height != new_height) {
img_width = new_width;
img_height = new_height;
sws.reset(
sws_getContext(
img_width, img_height, AV_PIX_FMT_BGR0,
session->ctx->width, session->ctx->height, session->sw_format,
SWS_LANCZOS | SWS_ACCURATE_RND,
nullptr, nullptr, nullptr));
sws_setColorspaceDetails(sws.get(), sws_getCoefficients(SWS_CS_DEFAULT), 0,
sws_getCoefficients(session->sws_color_format), config.encoderCscMode & 0x1,
0, 1 << 16, 1 << 16);
}
2020-04-02 20:13:44 +02:00
2020-04-08 02:15:08 +03:00
img_p = img.get();
}
2020-04-02 20:13:44 +02:00
else {
2020-04-07 18:57:59 +03:00
img_p = hwdevice_ctx->convert(*img);
if(!img_p) {
2020-04-06 23:15:03 +03:00
return;
}
2020-04-02 20:13:44 +02:00
}
2020-04-07 18:57:59 +03:00
encoder.img_to_frame(sws, *img_p, session->frame);
}
else if(images->running()) {
continue;
}
else {
break;
}
}
2020-04-10 15:39:50 +03:00
if(encode(frame_nr++, session->ctx, session->frame, packets, channel_data)) {
BOOST_LOG(fatal) << "Could not encode video packet"sv;
log_flush();
std::abort();
}
session->frame->pict_type = AV_PICTURE_TYPE_NONE;
}
}
void capture(
safe::signal_t *shutdown_event,
packet_queue_t packets,
idr_event_t idr_events,
config_t config,
void *channel_data) {
auto lg = util::fail_guard([&]() {
shutdown_event->raise(true);
});
const auto &encoder = encoders.front();
auto disp = platf::display(encoder.dev_type);
if(!disp) {
return;
}
auto pix_fmt = config.dynamicRange == 0 ? platf::pix_fmt_e::yuv420p : platf::pix_fmt_e::yuv420p10;
auto hwdevice_ctx = disp->make_hwdevice_ctx(config.width, config.height, pix_fmt);
if(!hwdevice_ctx) {
return;
}
auto session = make_session(encoder, config, hwdevice_ctx.get());
if(!session) {
return;
}
hwdevice_ctx->set_colorspace(session->sws_color_format, session->ctx->color_range);
auto img = disp->alloc_img();
if(disp->dummy_img(img.get())) {
return;
}
const platf::img_t* img_p = hwdevice_ctx->convert(*img);
if(!img_p) {
return;
}
sws_t sws;
encoder.img_to_frame(sws, *img_p, session->frame);
std::vector<std::shared_ptr<platf::img_t>> imgs(12);
for(auto &img : imgs) {
img = disp->alloc_img();
}
auto round_robin = util::make_round_robin<std::shared_ptr<platf::img_t>>(std::begin(imgs), std::end(imgs));
int frame_nr = 1;
int key_frame_nr = 1;
auto max_delay = 1000ms / config.framerate;
std::shared_ptr<platf::img_t> img_tmp;
auto next_frame = std::chrono::steady_clock::now();
while(!shutdown_event->peek()) {
if(idr_events->peek()) {
session->frame->pict_type = AV_PICTURE_TYPE_I;
auto event = idr_events->pop();
TUPLE_2D_REF(_, end, *event);
frame_nr = end;
key_frame_nr = end + config.framerate;
}
else if(frame_nr == key_frame_nr) {
session->frame->pict_type = AV_PICTURE_TYPE_I;
}
2020-04-10 15:39:50 +03:00
auto delay = std::max(0ms, std::chrono::duration_cast<std::chrono::milliseconds>(next_frame - std::chrono::steady_clock::now()));
auto status = disp->snapshot(round_robin->get(), delay, display_cursor);
switch(status) {
case platf::capture_e::reinit:
return;
case platf::capture_e::error:
return;
case platf::capture_e::timeout:
next_frame += max_delay;
if(!img_tmp && frame_nr > (key_frame_nr + config.framerate)) {
continue;
}
break;
case platf::capture_e::ok:
img_tmp = *round_robin++;
break;
2020-04-08 02:15:08 +03:00
}
2020-04-10 15:39:50 +03:00
if(img_tmp) {
img_p = hwdevice_ctx->convert(*img_tmp);
img_tmp.reset();
2020-04-08 02:15:08 +03:00
}
2020-04-10 15:39:50 +03:00
if(encode(frame_nr++, session->ctx, session->frame, packets, channel_data)) {
BOOST_LOG(fatal) << "Could not encode video packet"sv;
log_flush();
std::abort();
}
2020-03-25 10:51:32 +01:00
session->frame->pict_type = AV_PICTURE_TYPE_NONE;
}
}
2020-04-10 15:39:50 +03:00
void capture_async(
safe::signal_t *shutdown_event,
packet_queue_t packets,
idr_event_t idr_events,
config_t config,
void *channel_data) {
auto images = std::make_shared<img_event_t::element_type>();
2020-04-06 23:15:03 +03:00
auto lg = util::fail_guard([&]() {
images->stop();
shutdown_event->raise(true);
});
// Keep a reference counter to ensure the Fcapture thread only runs when other threads have a reference to the capture thread
static auto capture_thread = safe::make_shared<capture_thread_ctx_t>(start_capture, end_capture);
auto ref = capture_thread.ref();
if(!ref) {
return;
}
auto delay = std::chrono::floor<std::chrono::nanoseconds>(1s) / config.framerate;
ref->capture_ctx_queue->raise(capture_ctx_t {
images, delay
});
if(!ref->capture_ctx_queue->running()) {
return;
}
int frame_nr = 1;
int key_frame_nr = 1;
while(!shutdown_event->peek() && images->running()) {
// Wait for the display to be ready
2020-04-08 02:15:08 +03:00
std::shared_ptr<platf::display_t> display;
{
auto lg = ref->display_wp.lock();
if(ref->display_wp->expired()) {
continue;
}
2020-04-08 02:15:08 +03:00
display = ref->display_wp->lock();
}
2020-04-02 20:13:44 +02:00
2020-04-08 02:15:08 +03:00
auto pix_fmt = config.dynamicRange == 0 ? platf::pix_fmt_e::yuv420p : platf::pix_fmt_e::yuv420p10;
auto hwdevice_ctx = display->make_hwdevice_ctx(config.width, config.height, pix_fmt);
if(!hwdevice_ctx) {
return;
}
2020-04-10 15:39:50 +03:00
auto dummy_img = display->alloc_img();
if(display->dummy_img(dummy_img.get())) {
return;
}
images->raise(std::move(dummy_img));
2020-04-02 20:13:44 +02:00
encode_run(frame_nr, key_frame_nr, shutdown_event, packets, idr_events, images, config, hwdevice_ctx.get(), ref->reinit_event, *ref->encoder_p, channel_data);
}
}
bool validate_config(std::shared_ptr<platf::display_t> &disp, const encoder_t &encoder, const config_t &config) {
reset_display(disp, encoder.dev_type);
2020-04-02 20:13:44 +02:00
if(!disp) {
return false;
}
2020-04-02 20:13:44 +02:00
auto pix_fmt = config.dynamicRange == 0 ? platf::pix_fmt_e::yuv420p : platf::pix_fmt_e::yuv420p10;
auto hwdevice_ctx = disp->make_hwdevice_ctx(config.width, config.height, pix_fmt);
2020-04-06 23:15:03 +03:00
if(!hwdevice_ctx) {
return false;
}
2020-04-02 20:13:44 +02:00
2020-04-06 23:15:03 +03:00
auto session = make_session(encoder, config, hwdevice_ctx.get());
if(!session) {
return false;
}
2020-04-10 15:39:50 +03:00
hwdevice_ctx->set_colorspace(session->sws_color_format, session->ctx->color_range);
2020-04-02 20:13:44 +02:00
auto img = disp->alloc_img();
2020-04-10 15:39:50 +03:00
if(disp->dummy_img(img.get())) {
2020-04-06 23:15:03 +03:00
return false;
}
sws_t sws;
if(encoder.system_memory) {
sws.reset(sws_getContext(
img->width, img->height, AV_PIX_FMT_BGR0,
session->ctx->width, session->ctx->height, session->sw_format,
SWS_LANCZOS | SWS_ACCURATE_RND,
nullptr, nullptr, nullptr));
sws_setColorspaceDetails(sws.get(), sws_getCoefficients(SWS_CS_DEFAULT), 0,
sws_getCoefficients(session->sws_color_format), config.encoderCscMode & 0x1,
0, 1 << 16, 1 << 16);
2020-04-02 20:13:44 +02:00
encoder.img_to_frame(sws, *img, session->frame);
}
2020-04-02 20:13:44 +02:00
else {
auto converted_img = hwdevice_ctx->convert(*img);
2020-04-06 23:15:03 +03:00
if(!converted_img) {
return false;
}
2020-04-02 20:13:44 +02:00
encoder.img_to_frame(sws, *converted_img, session->frame);
}
session->frame->pict_type = AV_PICTURE_TYPE_I;
auto packets = std::make_shared<packet_queue_t::element_type>();
if(encode(1, session->ctx, session->frame, packets, nullptr)) {
return false;
}
return true;
}
bool validate_encoder(encoder_t &encoder) {
std::shared_ptr<platf::display_t> disp;
2020-04-07 14:54:56 +03:00
encoder.h264.capabilities.set();
encoder.hevc.capabilities.set();
// First, test encoder viability
config_t config_max_ref_frames {
1920, 1080,
60,
1000,
1,
1,
1,
0,
0
};
config_t config_autoselect {
1920, 1080,
60,
1000,
1,
2020-04-06 23:15:03 +03:00
0,
1,
0,
2020-04-02 20:13:44 +02:00
0
};
auto max_ref_frames_h264 = validate_config(disp, encoder, config_max_ref_frames);
auto autoselect_h264 = validate_config(disp, encoder, config_autoselect);
if(!max_ref_frames_h264 && !autoselect_h264) {
return false;
}
2020-04-07 14:54:56 +03:00
config_max_ref_frames.videoFormat = 1;
config_autoselect.videoFormat = 1;
auto max_ref_frames_hevc = validate_config(disp, encoder, config_max_ref_frames);
auto autoselect_hevc = validate_config(disp, encoder, config_autoselect);
encoder.h264[encoder_t::REF_FRAMES_RESTRICT] = max_ref_frames_h264;
encoder.h264[encoder_t::REF_FRAMES_AUTOSELECT] = autoselect_h264;
encoder.h264[encoder_t::PASSED] = true;
encoder.hevc[encoder_t::REF_FRAMES_RESTRICT] = max_ref_frames_hevc;
encoder.hevc[encoder_t::REF_FRAMES_AUTOSELECT] = autoselect_hevc;
encoder.hevc[encoder_t::PASSED] = max_ref_frames_hevc || autoselect_hevc;
std::vector<std::pair<encoder_t::flag_e, config_t>> configs;
for(auto &[flag, config] : configs) {
auto h264 = config;
auto hevc = config;
h264.videoFormat = 0;
hevc.videoFormat = 1;
2020-04-07 18:57:59 +03:00
encoder.h264[flag] = validate_config(disp, encoder, h264);
encoder.hevc[flag] = validate_config(disp, encoder, hevc);
}
return true;
}
void init() {
KITTY_WHILE_LOOP(auto pos = std::begin(encoders), pos != std::end(encoders), {
if(!validate_encoder(*pos)) {
pos = encoders.erase(pos);
continue;
}
++pos;
})
for(auto &encoder : encoders) {
BOOST_LOG(info) << "Found encoder ["sv << encoder.h264.name << ", "sv << encoder.hevc.name << ']';
}
}
2020-04-02 20:13:44 +02:00
void sw_img_to_frame(sws_t &sws, const platf::img_t &img, frame_t &frame) {
av_frame_make_writable(frame.get());
const int linesizes[2] {
img.row_pitch, 0
};
int ret = sws_scale(sws.get(), (std::uint8_t*const*)&img.data, linesizes, 0, img.height, frame->data, frame->linesize);
if(ret <= 0) {
BOOST_LOG(fatal) << "Couldn't convert image to required format and/or size"sv;
log_flush();
std::abort();
}
}
2020-04-02 20:13:44 +02:00
void nv_d3d_img_to_frame(sws_t &sws, const platf::img_t &img, frame_t &frame) {
2020-04-06 23:15:03 +03:00
// Need to have something refcounted
if(!frame->buf[0]) {
2020-04-08 02:15:08 +03:00
frame->buf[0] = av_buffer_allocz(sizeof(AVD3D11FrameDescriptor));
2020-04-06 23:15:03 +03:00
}
auto desc = (AVD3D11FrameDescriptor*)frame->buf[0]->data;
desc->texture = (ID3D11Texture2D*)img.data;
desc->index = 0;
frame->data[0] = img.data;
frame->data[1] = 0;
frame->linesize[0] = img.row_pitch;
frame->height = img.height;
frame->width = img.width;
}
2020-04-06 23:15:03 +03:00
2020-04-08 02:15:08 +03:00
void nvenc_lock(void *lock_p) {
}
void nvenc_unlock(void *lock_p) {
}
2020-04-06 23:15:03 +03:00
util::Either<buffer_t, int> nv_d3d_make_hwdevice_ctx(platf::hwdevice_ctx_t *hwdevice_ctx) {
buffer_t ctx_buf { av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA) };
auto ctx = (AVD3D11VADeviceContext*)((AVHWDeviceContext*)ctx_buf->data)->hwctx;
2020-04-08 02:15:08 +03:00
2020-04-06 23:15:03 +03:00
std::fill_n((std::uint8_t*)ctx, sizeof(AVD3D11VADeviceContext), 0);
std::swap(ctx->device, *(ID3D11Device**)&hwdevice_ctx->hwdevice);
auto err = av_hwdevice_ctx_init(ctx_buf.get());
if(err) {
char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
BOOST_LOG(error) << "Failed to create FFMpeg nvenc: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err);
return err;
}
return ctx_buf;
}
}