2020-01-13 00:52:30 +00:00
|
|
|
// FFMPEG Video Encoder Integration for OBS Studio
|
|
|
|
// Copyright (c) 2019 Michael Fabian Dirks <info@xaymar.com>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
#include "nvenc_h264_handler.hpp"
|
2020-04-05 04:13:14 +00:00
|
|
|
#include "strings.hpp"
|
2020-01-13 21:40:15 +00:00
|
|
|
#include "../codecs/h264.hpp"
|
2020-06-14 20:22:44 +00:00
|
|
|
#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" {
|
2020-03-27 19:06:27 +00:00
|
|
|
#ifdef _MSC_VER
|
2020-01-13 00:52:30 +00:00
|
|
|
#pragma warning(push)
|
2020-03-27 19:06:27 +00:00
|
|
|
#pragma warning(disable : 4242 4244 4365)
|
|
|
|
#endif
|
|
|
|
#include <obs-module.h>
|
2020-04-05 16:51:35 +00:00
|
|
|
#include <libavutil/opt.h>
|
2020-03-27 19:06:27 +00:00
|
|
|
#ifdef _MSC_VER
|
2020-01-13 00:52:30 +00:00
|
|
|
#pragma warning(pop)
|
2020-03-27 19:06:27 +00:00
|
|
|
#endif
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:09:22 +00:00
|
|
|
#define ST_KEY_PROFILE "H264.Profile"
|
|
|
|
#define ST_KEY_LEVEL "H264.Level"
|
2020-01-13 22:40:08 +00:00
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
using namespace streamfx::encoder::ffmpeg::handler;
|
|
|
|
using namespace streamfx::encoder::codec::h264;
|
2020-01-13 00:52:30 +00:00
|
|
|
|
2020-08-09 23:18:36 +00:00
|
|
|
void nvenc_h264_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.264/AVC (via FFmpeg)";
|
2020-08-09 23:18:36 +00:00
|
|
|
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_h264_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);
|
|
|
|
|
2022-02-20 18:45:41 +00:00
|
|
|
obs_data_set_default_string(settings, ST_KEY_PROFILE, "");
|
|
|
|
obs_data_set_default_string(settings, ST_KEY_LEVEL, "auto");
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
bool nvenc_h264_handler::has_keyframe_support(ffmpeg_factory*)
|
2020-04-17 08:24:13 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
bool nvenc_h264_handler::is_hardware_encoder(ffmpeg_factory*)
|
2020-04-17 08:24:13 +00:00
|
|
|
{
|
2020-04-05 16:52:06 +00:00
|
|
|
return true;
|
2020-04-17 08:24:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
bool nvenc_h264_handler::has_threading_support(ffmpeg_factory*)
|
2020-04-17 08:24:13 +00:00
|
|
|
{
|
2020-04-05 16:52:06 +00:00
|
|
|
return false;
|
2020-04-17 08:24:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
bool nvenc_h264_handler::has_pixel_format_support(ffmpeg_factory*)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
void nvenc_h264_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_h264_handler::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
|
|
|
nvenc::update(settings, codec, context);
|
|
|
|
|
2021-12-03 06:20:31 +00:00
|
|
|
if (!context->internal) {
|
2022-02-20 18:45:41 +00:00
|
|
|
if (auto v = obs_data_get_string(settings, ST_KEY_PROFILE); v && (strlen(v) > 0)) {
|
|
|
|
av_opt_set(context->priv_data, "profile", v, AV_OPT_SEARCH_CHILDREN);
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
2022-02-20 18:45:41 +00:00
|
|
|
if (auto v = obs_data_get_string(settings, ST_KEY_LEVEL); v && (strlen(v) > 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_h264_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_h264_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);
|
|
|
|
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_INFO("[%s] H.264/AVC:", codec->name);
|
2021-06-08 02:12:55 +00:00
|
|
|
::streamfx::ffmpeg::tools::print_av_option_string2(context, context->priv_data, "profile", " Profile",
|
|
|
|
[](int64_t v, std::string_view o) { return std::string(o); });
|
|
|
|
::streamfx::ffmpeg::tools::print_av_option_string2(context, context->priv_data, "level", " Level",
|
|
|
|
[](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_h264_handler::get_encoder_properties(obs_properties_t* props, const AVCodec* codec)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2022-02-17 03:30:17 +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();
|
2021-06-08 02:09:22 +00:00
|
|
|
obs_properties_add_group(props, S_CODEC_H264, D_TRANSLATE(S_CODEC_H264), OBS_GROUP_NORMAL, grp);
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-06-08 02:09:22 +00:00
|
|
|
auto p = obs_properties_add_list(grp, ST_KEY_PROFILE, D_TRANSLATE(S_CODEC_H264_PROFILE),
|
2022-02-20 18:45:41 +00:00
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
|
|
|
|
obs_property_list_add_string(p, D_TRANSLATE(S_STATE_DEFAULT), "");
|
|
|
|
streamfx::ffmpeg::tools::avoption_list_add_entries(
|
|
|
|
context->priv_data, "profile", [&p](const AVOption* opt) {
|
|
|
|
char buffer[1024];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s.%s\0", S_CODEC_H264_PROFILE, opt->name);
|
|
|
|
obs_property_list_add_string(p, D_TRANSLATE(buffer), opt->name);
|
|
|
|
});
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
{
|
2021-06-08 02:09:22 +00:00
|
|
|
auto p = obs_properties_add_list(grp, ST_KEY_LEVEL, D_TRANSLATE(S_CODEC_H264_LEVEL), OBS_COMBO_TYPE_LIST,
|
2022-02-20 18:45:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 03:30:17 +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_h264_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);
|
|
|
|
}
|
2020-07-05 22:10:04 +00:00
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void streamfx::encoder::ffmpeg::handler::nvenc_h264_handler::migrate(obs_data_t* settings, uint64_t version,
|
2020-07-05 22:10:04 +00:00
|
|
|
const AVCodec* codec, AVCodecContext* context)
|
|
|
|
{
|
|
|
|
nvenc::migrate(settings, version, codec, context);
|
2022-02-20 18:45:41 +00:00
|
|
|
|
|
|
|
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 = 3;
|
|
|
|
|
|
|
|
std::map<int64_t, std::string> preset{
|
|
|
|
{0, "baseline"}, {1, "baseline"}, {2, "main"}, {3, "high"}, {4, "high444p"},
|
|
|
|
};
|
|
|
|
if (auto k = preset.find(v); k != preset.end()) {
|
|
|
|
obs_data_set_string(settings, ST_KEY_PROFILE, k->second.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Level
|
|
|
|
obs_data_set_string(settings, ST_KEY_LEVEL, "auto");
|
|
|
|
}
|
2020-07-05 22:10:04 +00:00
|
|
|
}
|
2021-12-03 06:20:31 +00:00
|
|
|
|
|
|
|
bool nvenc_h264_handler::supports_reconfigure(ffmpeg_factory* instance, bool& threads, bool& gpu, bool& keyframes)
|
|
|
|
{
|
|
|
|
threads = false;
|
|
|
|
gpu = false;
|
|
|
|
keyframes = false;
|
|
|
|
return true;
|
|
|
|
}
|