obs-StreamFX/source/encoders/handlers/nvenc_hevc_handler.cpp

211 lines
6.8 KiB
C++
Raw Normal View History

// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2020-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// Copyright (C) 2022 lainon <GermanAizek@yandex.ru>
// AUTOGENERATED COPYRIGHT HEADER END
2020-01-13 00:52:30 +00:00
#include "nvenc_hevc_handler.hpp"
#include "common.hpp"
#include "strings.hpp"
2020-01-13 21:40:15 +00:00
#include "../codecs/hevc.hpp"
#include "../encoder-ffmpeg.hpp"
2020-01-13 00:52:30 +00:00
#include "ffmpeg/tools.hpp"
#include "nvenc_shared.hpp"
#include "plugin.hpp"
extern "C" {
#include "warning-disable.hpp"
2020-01-13 00:52:30 +00:00
#include <libavutil/opt.h>
#include "warning-enable.hpp"
2020-01-13 00:52:30 +00:00
}
#define ST_KEY_PROFILE "H265.Profile"
#define ST_KEY_TIER "H265.Tier"
#define ST_KEY_LEVEL "H265.Level"
2020-01-13 22:40:08 +00:00
using namespace streamfx::encoder::ffmpeg::handler;
using namespace streamfx::encoder::codec::hevc;
2020-01-13 00:52:30 +00:00
void nvenc_hevc_handler::adjust_info(ffmpeg_factory* fac, const AVCodec*, std::string&, std::string& name, std::string&)
2020-01-13 00:52:30 +00:00
{
2020-06-22 20:59:06 +00:00
name = "NVIDIA NVENC H.265/HEVC (via FFmpeg)";
if (!nvenc::is_available())
fac->get_info()->caps |= OBS_ENCODER_CAP_DEPRECATED;
2020-01-13 00:52:30 +00:00
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::get_defaults(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context, bool)
2020-01-13 00:52:30 +00:00
{
nvenc::get_defaults(settings, codec, context);
obs_data_set_default_string(settings, ST_KEY_PROFILE, "");
obs_data_set_default_string(settings, ST_KEY_TIER, "");
obs_data_set_default_string(settings, ST_KEY_LEVEL, "auto");
2020-01-13 00:52:30 +00:00
}
bool nvenc_hevc_handler::has_keyframe_support(ffmpeg_factory*)
{
return true;
}
bool nvenc_hevc_handler::is_hardware_encoder(ffmpeg_factory* instance)
{
return true;
}
bool nvenc_hevc_handler::has_threading_support(ffmpeg_factory* instance)
{
return false;
}
bool nvenc_hevc_handler::has_pixel_format_support(ffmpeg_factory* instance)
2020-01-13 00:52:30 +00:00
{
return true;
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::get_properties(obs_properties_t* props, const AVCodec* codec, AVCodecContext* context, bool)
2020-01-13 00:52:30 +00:00
{
if (!context) {
this->get_encoder_properties(props, codec);
} else {
this->get_runtime_properties(props, codec, context);
}
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
2020-01-13 00:52:30 +00:00
{
nvenc::update(settings, codec, context);
if (!context->internal) {
if (const char* v = obs_data_get_string(settings, ST_KEY_PROFILE); v && (v[0] != '\0')) {
av_opt_set(context->priv_data, "profile", v, AV_OPT_SEARCH_CHILDREN);
2020-01-13 00:52:30 +00:00
}
if (const char* v = obs_data_get_string(settings, ST_KEY_TIER); v && (v[0] != '\0')) {
av_opt_set(context->priv_data, "tier", v, AV_OPT_SEARCH_CHILDREN);
2020-01-13 00:52:30 +00:00
}
if (const char* v = obs_data_get_string(settings, ST_KEY_LEVEL); v && (v[0] != '\0')) {
av_opt_set(context->priv_data, "level", v, AV_OPT_SEARCH_CHILDREN);
2020-01-13 00:52:30 +00:00
}
}
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::override_update(ffmpeg_instance* instance, obs_data_t* settings)
2020-01-13 00:52:30 +00:00
{
nvenc::override_update(instance, settings);
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
2020-01-13 00:52:30 +00:00
{
nvenc::log_options(settings, codec, context);
DLOG_INFO("[%s] H.265/HEVC:", codec->name);
::streamfx::ffmpeg::tools::print_av_option_string2(context, "profile", " Profile", [](int64_t v, std::string_view o) { return std::string(o); });
::streamfx::ffmpeg::tools::print_av_option_string2(context, "level", " Level", [](int64_t v, std::string_view o) { return std::string(o); });
::streamfx::ffmpeg::tools::print_av_option_string2(context, "tier", " Tier", [](int64_t v, std::string_view o) { return std::string(o); });
2020-01-13 00:52:30 +00:00
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::get_encoder_properties(obs_properties_t* props, const AVCodec* codec)
2020-01-13 00:52:30 +00:00
{
AVCodecContext* context = avcodec_alloc_context3(codec);
if (!context->priv_data) {
avcodec_free_context(&context);
return;
}
nvenc::get_properties_pre(props, codec, context);
2020-01-13 00:52:30 +00:00
{
obs_properties_t* grp = props;
2021-06-08 02:18:02 +00:00
if (!streamfx::util::are_property_groups_broken()) {
2020-01-13 00:52:30 +00:00
grp = obs_properties_create();
obs_properties_add_group(props, S_CODEC_HEVC, D_TRANSLATE(S_CODEC_HEVC), OBS_GROUP_NORMAL, grp);
2020-01-13 00:52:30 +00:00
}
{
auto p = obs_properties_add_list(grp, ST_KEY_PROFILE, D_TRANSLATE(S_CODEC_HEVC_PROFILE), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DEFAULT), -1);
streamfx::ffmpeg::tools::avoption_list_add_entries(context->priv_data, "profile", [&p](const AVOption* opt) {
char buffer[1024];
snprintf(buffer, sizeof(buffer), "%s.%s", S_CODEC_HEVC_PROFILE, opt->name);
obs_property_list_add_string(p, D_TRANSLATE(buffer), opt->name);
});
2020-01-13 00:52:30 +00:00
}
{
auto p = obs_properties_add_list(grp, ST_KEY_TIER, D_TRANSLATE(S_CODEC_HEVC_TIER), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DEFAULT), -1);
streamfx::ffmpeg::tools::avoption_list_add_entries(context->priv_data, "tier", [&p](const AVOption* opt) {
char buffer[1024];
snprintf(buffer, sizeof(buffer), "%s.%s", S_CODEC_HEVC_TIER, opt->name);
obs_property_list_add_string(p, D_TRANSLATE(buffer), opt->name);
});
2020-01-13 00:52:30 +00:00
}
{
auto p = obs_properties_add_list(grp, ST_KEY_LEVEL, D_TRANSLATE(S_CODEC_HEVC_LEVEL), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
streamfx::ffmpeg::tools::avoption_list_add_entries(context->priv_data, "level", [&p](const AVOption* opt) {
if (opt->default_val.i64 == 0) {
obs_property_list_add_string(p, D_TRANSLATE(S_STATE_AUTOMATIC), "auto");
} else {
obs_property_list_add_string(p, opt->name, opt->name);
}
});
2020-01-13 00:52:30 +00:00
}
}
nvenc::get_properties_post(props, codec, context);
if (context) {
avcodec_free_context(&context);
}
2020-01-13 00:52:30 +00:00
}
2020-01-13 21:40:15 +00:00
void nvenc_hevc_handler::get_runtime_properties(obs_properties_t* props, const AVCodec* codec, AVCodecContext* context)
2020-01-13 00:52:30 +00:00
{
nvenc::get_runtime_properties(props, codec, context);
}
void streamfx::encoder::ffmpeg::handler::nvenc_hevc_handler::migrate(obs_data_t* settings, uint64_t version, const AVCodec* codec, AVCodecContext* context)
{
nvenc::migrate(settings, version, codec, context);
if (version < STREAMFX_MAKE_VERSION(0, 11, 1, 0)) {
// Profile
if (auto v = obs_data_get_int(settings, ST_KEY_PROFILE); v != -1) {
if (!obs_data_has_user_value(settings, ST_KEY_PROFILE))
v = 0;
std::map<int64_t, std::string> preset{
{0, "main"},
{1, "main10"},
{2, "rext"},
};
if (auto k = preset.find(v); k != preset.end()) {
obs_data_set_string(settings, ST_KEY_PROFILE, k->second.data());
}
}
// Tier
if (auto v = obs_data_get_int(settings, ST_KEY_TIER); v != -1) {
if (!obs_data_has_user_value(settings, ST_KEY_TIER))
v = 0;
std::map<int64_t, std::string> preset{
{0, "main"},
{1, "high"},
};
if (auto k = preset.find(v); k != preset.end()) {
obs_data_set_string(settings, ST_KEY_TIER, k->second.data());
}
}
// Level
obs_data_set_string(settings, ST_KEY_LEVEL, "auto");
}
}
bool nvenc_hevc_handler::supports_reconfigure(ffmpeg_factory* instance, bool& threads, bool& gpu, bool& keyframes)
{
threads = false;
gpu = false;
keyframes = false;
return true;
}