project: Merged FFmpeg Encoders Step 3

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-01-13 23:40:08 +01:00
parent 0a16ce07dc
commit a33dd83d72
6 changed files with 1764 additions and 172 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,221 @@
// 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.
#pragma once
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <stack>
#include <string>
#include <thread>
#include <vector>
#include "ffmpeg/avframe-queue.hpp"
#include "ffmpeg/hwapi/base.hpp"
#include "ffmpeg/swscale.hpp"
#include "handlers/handler.hpp"
extern "C" {
#include <obs-properties.h>
#include <obs.h>
#pragma warning(push)
#pragma warning(disable : 4244)
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
#pragma warning(pop)
}
#define S_RATECONTROL "FFmpeg.RateControl"
#define S_RATECONTROL_MODE "FFmpeg.RateControl.Mode"
#define S_RATECONTROL_MODE_(x) "FFmpeg.RateControl.Mode." D_VSTR(x)
#define S_RATECONTROL_BITRATE_TARGET "FFmpeg.RateControl.Bitrate.Target"
#define S_RATECONTROL_BITRATE_MINIMUM "FFmpeg.RateControl.Bitrate.Minimum"
#define S_RATECONTROL_BITRATE_MAXIMUM "FFmpeg.RateControl.Bitrate.Maximum"
#define S_RATECONTROL_BUFFERSIZE "FFmpeg.RateControl.BufferSize"
#define S_RATECONTROL_QUALITY_TARGET "RateControl.Quality.Target"
#define S_RATECONTROL_QUALITY_MINIMUM "RateControl.Quality.Minimum"
#define S_RATECONTROL_QUALITY_MAXIMUM "RateControl.Quality.Maximum"
#define S_RATECONTROL_QP_I "RateControl.QP.I"
#define S_RATECONTROL_QP_P "RateControl.QP.P"
#define S_RATECONTROL_QP_B "RateControl.QP.B"
#define S_RATECONTROL_QP_I_INITIAL "RateControl.QP.I.Initial"
#define S_RATECONTROL_QP_P_INITIAL "RateControl.QP.P.Initial"
#define S_RATECONTROL_QP_B_INITIAL "RateControl.QP.B.Initial"
#define S_KEYFRAMES "KeyFrames"
#define S_KEYFRAMES_INTERVALTYPE "KeyFrames.IntervalType"
#define S_KEYFRAMES_INTERVALTYPE_(x) "KeyFrames.IntervalType." D_VSTR(x)
#define S_KEYFRAMES_INTERVAL "KeyFrames.Interval"
#define S_KEYFRAMES_INTERVAL_SECONDS "KeyFrames.Interval.Seconds"
#define S_KEYFRAMES_INTERVAL_FRAMES "KeyFrames.Interval.Frames"
namespace encoder::ffmpeg {
class ffmpeg_factory;
class ffmpeg_manager {
static ::std::shared_ptr<ffmpeg_manager> _instance;
public: // Singleton
static void initialize()
{
_instance = ::std::make_shared<ffmpeg_manager>();
}
static void finalize()
{
_instance.reset();
}
static std::shared_ptr<ffmpeg_manager> get()
{
return _instance;
}
private:
std::map<const AVCodec*, std::shared_ptr<ffmpeg_factory>> _factories;
std::map<std::string, std::shared_ptr<handler::handler>> _handlers;
std::shared_ptr<handler::handler> _debug_handler;
public:
ffmpeg_manager();
~ffmpeg_manager();
void register_handler(std::string const codec, std::shared_ptr<handler::handler> const handler);
std::shared_ptr<handler::handler> const get_handler(std::string const codec);
bool has_handler(std::string const codec);
};
struct ffmpeg_info {
std::string uid;
std::string codec;
std::string readable_name;
obs_encoder_info oei = {0};
};
class ffmpeg_factory {
ffmpeg_info info;
ffmpeg_info info_fallback;
const AVCodec* avcodec_ptr;
std::shared_ptr<handler::handler> _handler;
public:
ffmpeg_factory(const AVCodec* codec);
virtual ~ffmpeg_factory();
void register_encoder();
void get_defaults(obs_data_t* settings, bool hw_encoder = false);
void get_properties(obs_properties_t* props, bool hw_encoder = false);
const AVCodec* get_avcodec();
const ffmpeg_info& get_info();
const ffmpeg_info& get_fallback();
};
class ffmpeg_instance {
obs_encoder_t* _self;
ffmpeg_factory* _factory;
const AVCodec* _codec;
AVCodecContext* _context;
std::shared_ptr<handler::handler> _handler;
std::shared_ptr<::ffmpeg::hwapi::base> _hwapi;
std::shared_ptr<::ffmpeg::hwapi::instance> _hwinst;
::ffmpeg::swscale _swscale;
AVPacket _current_packet;
size_t _lag_in_frames;
size_t _count_send_frames;
// Extra Data
bool _have_first_frame;
std::vector<uint8_t> _extra_data;
std::vector<uint8_t> _sei_data;
// Frame Stack and Queue
std::stack<std::shared_ptr<AVFrame>> _free_frames;
std::queue<std::shared_ptr<AVFrame>> _used_frames;
std::chrono::high_resolution_clock::time_point _free_frames_last_used;
void initialize_sw(obs_data_t* settings);
void initialize_hw(obs_data_t* settings);
void push_free_frame(std::shared_ptr<AVFrame> frame);
std::shared_ptr<AVFrame> pop_free_frame();
void push_used_frame(std::shared_ptr<AVFrame> frame);
std::shared_ptr<AVFrame> pop_used_frame();
public:
ffmpeg_instance(obs_data_t* settings, obs_encoder_t* encoder, bool is_texture_encode = false);
virtual ~ffmpeg_instance();
public: // OBS API
// Shared
void get_properties(obs_properties_t* props, bool hw_encode = false);
bool update(obs_data_t* settings);
// Audio only
void get_audio_info(struct audio_convert_info* info);
size_t get_frame_size();
bool audio_encode(struct encoder_frame* frame, struct encoder_packet* packet, bool* received_packet);
// Video only
void get_video_info(struct video_scale_info* info);
bool get_sei_data(uint8_t** sei_data, size_t* size);
bool get_extra_data(uint8_t** extra_data, size_t* size);
bool video_encode(struct encoder_frame* frame, struct encoder_packet* packet, bool* received_packet);
bool video_encode_texture(uint32_t handle, int64_t pts, uint64_t lock_key, uint64_t* next_key,
struct encoder_packet* packet, bool* received_packet);
int receive_packet(bool* received_packet, struct encoder_packet* packet);
int send_frame(std::shared_ptr<AVFrame> frame);
bool encode_avframe(std::shared_ptr<AVFrame> frame, struct encoder_packet* packet, bool* received_packet);
public: // Handler API
bool is_hardware_encode();
const AVCodec* get_avcodec();
const AVCodecContext* get_avcodeccontext();
void parse_ffmpeg_commandline(std::string text);
};
} // namespace encoder::ffmpeg

View file

@ -36,6 +36,9 @@ extern "C" {
#pragma warning(pop)
}
#define KEY_PROFILE "H264.Profile"
#define KEY_LEVEL "H264.Level"
using namespace encoder::ffmpeg::handler;
using namespace encoder::codec::h264;
@ -55,16 +58,17 @@ std::map<level, std::string> levels{
void nvenc_h264_handler::adjust_encoder_info(ffmpeg_factory*, ffmpeg_info* main, ffmpeg_info* fallback)
{
main->readable_name = "H.264/AVC NVidia NVENC (Hardware)";
fallback->readable_name = "H.264/AVC NVidia NVENC (Software)";
main->readable_name = "H.264/AVC NVidia NVENC";
fallback->readable_name = "H.264/AVC NVidia NVENC (Software Fallback)";
fallback->oei.caps |= OBS_ENCODER_CAP_DEPRECATED;
}
void nvenc_h264_handler::get_defaults(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context, bool)
{
nvenc::get_defaults(settings, codec, context);
obs_data_set_default_int(settings, P_H264_PROFILE, static_cast<int64_t>(profile::HIGH));
obs_data_set_default_int(settings, P_H264_LEVEL, static_cast<int64_t>(level::UNKNOWN));
obs_data_set_default_int(settings, KEY_PROFILE, static_cast<int64_t>(profile::HIGH));
obs_data_set_default_int(settings, KEY_LEVEL, static_cast<int64_t>(level::UNKNOWN));
}
bool nvenc_h264_handler::has_keyframe_support(ffmpeg_instance*)
@ -86,14 +90,14 @@ void nvenc_h264_handler::update(obs_data_t* settings, const AVCodec* codec, AVCo
nvenc::update(settings, codec, context);
{
auto found = profiles.find(static_cast<profile>(obs_data_get_int(settings, P_H264_PROFILE)));
auto found = profiles.find(static_cast<profile>(obs_data_get_int(settings, KEY_PROFILE)));
if (found != profiles.end()) {
av_opt_set(context->priv_data, "profile", found->second.c_str(), 0);
}
}
{
auto found = levels.find(static_cast<level>(obs_data_get_int(settings, P_H264_LEVEL)));
auto found = levels.find(static_cast<level>(obs_data_get_int(settings, KEY_LEVEL)));
if (found != levels.end()) {
av_opt_set(context->priv_data, "level", found->second.c_str(), 0);
} else {
@ -111,7 +115,7 @@ void nvenc_h264_handler::log_options(obs_data_t* settings, const AVCodec* codec,
{
nvenc::log_options(settings, codec, context);
LOG_INFO("[%s] H.265/HEVC:", codec->name);
LOG_INFO("[%s] H.264/AVC:", codec->name);
::ffmpeg::tools::print_av_option_string(context, "profile", " Profile", [](int64_t v) {
profile val = static_cast<profile>(v);
auto index = profiles.find(val);
@ -140,7 +144,7 @@ void nvenc_h264_handler::get_encoder_properties(obs_properties_t* props, const A
}
{
auto p = obs_properties_add_list(grp, P_H264_PROFILE, D_TRANSLATE(P_H264_PROFILE), OBS_COMBO_TYPE_LIST,
auto p = obs_properties_add_list(grp, KEY_PROFILE, D_TRANSLATE(P_H264_PROFILE), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(P_H264_PROFILE)));
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DEFAULT), static_cast<int64_t>(profile::UNKNOWN));
@ -150,7 +154,7 @@ void nvenc_h264_handler::get_encoder_properties(obs_properties_t* props, const A
}
}
{
auto p = obs_properties_add_list(grp, P_H264_LEVEL, D_TRANSLATE(P_H264_LEVEL), OBS_COMBO_TYPE_LIST,
auto p = obs_properties_add_list(grp, KEY_LEVEL, D_TRANSLATE(P_H264_LEVEL), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(P_H264_LEVEL)));
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_AUTOMATIC), static_cast<int64_t>(level::UNKNOWN));

View file

@ -36,6 +36,10 @@ extern "C" {
#pragma warning(pop)
}
#define KEY_PROFILE "H265.Profile"
#define KEY_TIER "H265.Tier"
#define KEY_LEVEL "H265.Level"
using namespace encoder::ffmpeg::handler;
using namespace encoder::codec::hevc;
@ -58,17 +62,18 @@ std::map<level, std::string> levels{
void nvenc_hevc_handler::adjust_encoder_info(ffmpeg_factory*, ffmpeg_info* main, ffmpeg_info* fallback)
{
main->readable_name = "H.265/HEVC Nvidia NVENC (Hardware)";
fallback->readable_name = "H.265/HEVC Nvidia NVENC (Software)";
main->readable_name = "H.265/HEVC Nvidia NVENC";
fallback->readable_name = "H.265/HEVC Nvidia NVENC (Software Fallback)";
fallback->oei.caps |= OBS_ENCODER_CAP_DEPRECATED;
}
void nvenc_hevc_handler::get_defaults(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context, bool)
{
nvenc::get_defaults(settings, codec, context);
obs_data_set_default_int(settings, P_HEVC_PROFILE, static_cast<int64_t>(profile::MAIN));
obs_data_set_default_int(settings, P_HEVC_TIER, static_cast<int64_t>(profile::MAIN));
obs_data_set_default_int(settings, P_HEVC_LEVEL, static_cast<int64_t>(level::UNKNOWN));
obs_data_set_default_int(settings, KEY_PROFILE, static_cast<int64_t>(profile::MAIN));
obs_data_set_default_int(settings, KEY_TIER, static_cast<int64_t>(profile::MAIN));
obs_data_set_default_int(settings, KEY_LEVEL, static_cast<int64_t>(level::UNKNOWN));
}
bool nvenc_hevc_handler::has_keyframe_support(ffmpeg_instance*)
@ -90,19 +95,19 @@ void nvenc_hevc_handler::update(obs_data_t* settings, const AVCodec* codec, AVCo
nvenc::update(settings, codec, context);
{ // HEVC Options
auto found = profiles.find(static_cast<profile>(obs_data_get_int(settings, P_HEVC_PROFILE)));
auto found = profiles.find(static_cast<profile>(obs_data_get_int(settings, KEY_PROFILE)));
if (found != profiles.end()) {
av_opt_set(context->priv_data, "profile", found->second.c_str(), 0);
}
}
{
auto found = tiers.find(static_cast<tier>(obs_data_get_int(settings, P_HEVC_TIER)));
auto found = tiers.find(static_cast<tier>(obs_data_get_int(settings, KEY_TIER)));
if (found != tiers.end()) {
av_opt_set(context->priv_data, "tier", found->second.c_str(), 0);
}
}
{
auto found = levels.find(static_cast<level>(obs_data_get_int(settings, P_HEVC_LEVEL)));
auto found = levels.find(static_cast<level>(obs_data_get_int(settings, KEY_LEVEL)));
if (found != levels.end()) {
av_opt_set(context->priv_data, "level", found->second.c_str(), 0);
} else {
@ -156,7 +161,7 @@ void nvenc_hevc_handler::get_encoder_properties(obs_properties_t* props, const A
}
{
auto p = obs_properties_add_list(grp, P_HEVC_PROFILE, D_TRANSLATE(P_HEVC_PROFILE), OBS_COMBO_TYPE_LIST,
auto p = obs_properties_add_list(grp, KEY_PROFILE, D_TRANSLATE(P_HEVC_PROFILE), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(P_HEVC_PROFILE)));
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DEFAULT), static_cast<int64_t>(profile::UNKNOWN));
@ -166,7 +171,7 @@ void nvenc_hevc_handler::get_encoder_properties(obs_properties_t* props, const A
}
}
{
auto p = obs_properties_add_list(grp, P_HEVC_TIER, D_TRANSLATE(P_HEVC_TIER), OBS_COMBO_TYPE_LIST,
auto p = obs_properties_add_list(grp, KEY_TIER, D_TRANSLATE(P_HEVC_TIER), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(P_HEVC_TIER)));
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DEFAULT), static_cast<int64_t>(tier::UNKNOWN));
@ -176,7 +181,7 @@ void nvenc_hevc_handler::get_encoder_properties(obs_properties_t* props, const A
}
}
{
auto p = obs_properties_add_list(grp, P_HEVC_LEVEL, D_TRANSLATE(P_HEVC_LEVEL), OBS_COMBO_TYPE_LIST,
auto p = obs_properties_add_list(grp, KEY_LEVEL, D_TRANSLATE(P_HEVC_LEVEL), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(P_HEVC_LEVEL)));
obs_property_list_add_int(p, D_TRANSLATE(S_STATE_AUTOMATIC), static_cast<int64_t>(level::UNKNOWN));

View file

@ -36,45 +36,60 @@ extern "C" {
#pragma warning(pop)
}
#define ST_PRESET "NVENC.Preset"
#define ST_PRESET "Encoder.NVENC.Preset"
#define ST_PRESET_(x) ST_PRESET "." D_VSTR(x)
#define ST_RATECONTROL "NVENC.RateControl"
#define ST_RATECONTROL "Encoder.NVENC.RateControl"
#define ST_RATECONTROL_MODE ST_RATECONTROL ".Mode"
#define ST_RATECONTROL_MODE_(x) ST_RATECONTROL_MODE "." D_VSTR(x)
#define ST_RATECONTROL_TWOPASS ST_RATECONTROL ".TwoPass"
#define ST_RATECONTROL_LOOKAHEAD ST_RATECONTROL ".LookAhead"
#define ST_RATECONTROL_ADAPTIVEI ST_RATECONTROL ".AdaptiveI"
#define ST_RATECONTROL_ADAPTIVEB ST_RATECONTROL ".AdaptiveB"
#define ST_RATECONTROL_BITRATE ST_RATECONTROL ".Bitrate"
#define ST_RATECONTROL_BITRATE_TARGET ST_RATECONTROL_BITRATE ".Target"
#define ST_RATECONTROL_BITRATE_MAXIMUM ST_RATECONTROL_BITRATE ".Maximum"
#define ST_RATECONTROL_QUALITY ST_RATECONTROL ".Quality"
#define ST_RATECONTROL_QUALITY_MINIMUM ST_RATECONTROL_QUALITY ".Minimum"
#define ST_RATECONTROL_QUALITY_MAXIMUM ST_RATECONTROL_QUALITY ".Maximum"
#define ST_RATECONTROL_QUALITY_TARGET ST_RATECONTROL_QUALITY ".Target"
#define ST_RATECONTROL_QP ST_RATECONTROL ".QP"
#define ST_RATECONTROL_QP_I ST_RATECONTROL_QP ".I"
#define ST_RATECONTROL_QP_I_INITIAL ST_RATECONTROL_QP_I ".Initial"
#define ST_RATECONTROL_QP_P ST_RATECONTROL_QP ".P"
#define ST_RATECONTROL_QP_P_INITIAL ST_RATECONTROL_QP_P ".Initial"
#define ST_RATECONTROL_QP_B ST_RATECONTROL_QP ".B"
#define ST_RATECONTROL_QP_B_INITIAL ST_RATECONTROL_QP_B ".Initial"
#define ST_AQ "NVENC.AQ"
#define ST_AQ "Encoder.NVENC.AQ"
#define ST_AQ_SPATIAL ST_AQ ".Spatial"
#define ST_AQ_TEMPORAL ST_AQ ".Temporal"
#define ST_AQ_STRENGTH ST_AQ ".Strength"
#define ST_OTHER "NVENC.Other"
#define ST_OTHER "Encoder.NVENC.Other"
#define ST_OTHER_BFRAMES ST_OTHER ".BFrames"
#define ST_OTHER_BFRAME_REFERENCEMODE ST_OTHER ".BFrameReferenceMode"
#define ST_OTHER_BFRAMEREFERENCEMODE ST_OTHER ".BFrameReferenceMode"
#define ST_OTHER_ZEROLATENCY ST_OTHER ".ZeroLatency"
#define ST_OTHER_WEIGHTED_PREDICTION ST_OTHER ".WeightedPrediction"
#define ST_OTHER_NONREFERENCE_PFRAMES ST_OTHER ".NonReferencePFrames"
#define ST_OTHER_WEIGHTEDPREDICTION ST_OTHER ".WeightedPrediction"
#define ST_OTHER_NONREFERENCEPFRAMES ST_OTHER ".NonReferencePFrames"
#define KEY_PRESET "Preset"
#define KEY_RATECONTROL_MODE "RateControl.Mode"
#define KEY_RATECONTROL_TWOPASS "RateControl.TwoPass"
#define KEY_RATECONTROL_LOOKAHEAD "RateControl.LookAhead"
#define KEY_RATECONTROL_ADAPTIVEI "RateControl.AdaptiveI"
#define KEY_RATECONTROL_ADAPTIVEB "RateControl.AdaptiveB"
#define KEY_RATECONTROL_BITRATE_TARGET "RateControl.Bitrate.Target"
#define KEY_RATECONTROL_BITRATE_MAXIMUM "RateControl.Bitrate.Maximum"
#define KEY_RATECONTROL_QUALITY "RateControl.Quality"
#define KEY_RATECONTROL_QUALITY_TARGET "RateControl.Quality.Target"
#define KEY_RATECONTROL_QUALITY_MINIMUM "RateControl.Quality.Minimum"
#define KEY_RATECONTROL_QUALITY_MAXIMUM "RateControl.Quality.Maximum"
#define KEY_RATECONTROL_QP_I "RateControl.QP.I"
#define KEY_RATECONTROL_QP_P "RateControl.QP.P"
#define KEY_RATECONTROL_QP_B "RateControl.QP.B"
#define KEY_AQ_SPATIAL "AQ.Spatial"
#define KEY_AQ_TEMPORAL "AQ.Temporal"
#define KEY_AQ_STRENGTH "AQ.Strength"
#define KEY_OTHER_BFRAMES "Other.BFrames"
#define KEY_OTHER_BFRAMEREFERENCEMODE "Other.BFrameReferenceMode"
#define KEY_OTHER_ZEROLATENCY "Other.ZeroLatency"
#define KEY_OTHER_WEIGHTEDPREDICTION "Other.WeightedPrediction"
#define KEY_OTHER_NONREFERENCEPFRAMES "Other.NonReferencePFrames"
using namespace encoder::ffmpeg::handler;
using namespace ffmpeg;
@ -126,8 +141,8 @@ std::map<nvenc::ratecontrolmode, std::string> nvenc::ratecontrolmode_to_opt{
std::map<nvenc::b_ref_mode, std::string> nvenc::b_ref_modes{
{nvenc::b_ref_mode::DISABLED, S_STATE_DISABLED},
{nvenc::b_ref_mode::EACH, ST_OTHER_BFRAME_REFERENCEMODE ".Each"},
{nvenc::b_ref_mode::MIDDLE, ST_OTHER_BFRAME_REFERENCEMODE ".Middle"},
{nvenc::b_ref_mode::EACH, ST_OTHER_BFRAMEREFERENCEMODE ".Each"},
{nvenc::b_ref_mode::MIDDLE, ST_OTHER_BFRAMEREFERENCEMODE ".Middle"},
};
std::map<nvenc::b_ref_mode, std::string> nvenc::b_ref_mode_to_opt{
@ -168,38 +183,36 @@ void nvenc::override_update(ffmpeg_instance* instance, obs_data_t*)
void nvenc::get_defaults(obs_data_t* settings, const AVCodec*, AVCodecContext*)
{
obs_data_set_default_int(settings, ST_PRESET, static_cast<int64_t>(nvenc::preset::DEFAULT));
obs_data_set_default_int(settings, KEY_PRESET, static_cast<int64_t>(nvenc::preset::DEFAULT));
obs_data_set_default_int(settings, ST_RATECONTROL_MODE, static_cast<int64_t>(ratecontrolmode::CBR_HQ));
obs_data_set_default_int(settings, ST_RATECONTROL_TWOPASS, -1);
obs_data_set_default_int(settings, ST_RATECONTROL_LOOKAHEAD, 0);
obs_data_set_default_int(settings, ST_RATECONTROL_ADAPTIVEI, -1);
obs_data_set_default_int(settings, ST_RATECONTROL_ADAPTIVEB, -1);
obs_data_set_default_int(settings, KEY_RATECONTROL_MODE, static_cast<int64_t>(ratecontrolmode::CBR_HQ));
obs_data_set_default_int(settings, KEY_RATECONTROL_TWOPASS, -1);
obs_data_set_default_int(settings, KEY_RATECONTROL_LOOKAHEAD, 0);
obs_data_set_default_int(settings, KEY_RATECONTROL_ADAPTIVEI, -1);
obs_data_set_default_int(settings, KEY_RATECONTROL_ADAPTIVEB, -1);
obs_data_set_default_int(settings, ST_RATECONTROL_BITRATE_TARGET, 6000);
obs_data_set_default_int(settings, ST_RATECONTROL_BITRATE_MAXIMUM, 6000);
obs_data_set_default_int(settings, KEY_RATECONTROL_BITRATE_TARGET, 6000);
obs_data_set_default_int(settings, KEY_RATECONTROL_BITRATE_MAXIMUM, 6000);
obs_data_set_default_int(settings, S_RATECONTROL_BUFFERSIZE, 12000);
obs_data_set_default_int(settings, ST_RATECONTROL_QUALITY_MINIMUM, 51);
obs_data_set_default_int(settings, ST_RATECONTROL_QUALITY_MAXIMUM, -1);
obs_data_set_default_int(settings, ST_RATECONTROL_QUALITY_TARGET, 0);
obs_data_set_default_bool(settings, KEY_RATECONTROL_QUALITY, false);
obs_data_set_default_int(settings, KEY_RATECONTROL_QUALITY_MINIMUM, 51);
obs_data_set_default_int(settings, KEY_RATECONTROL_QUALITY_MAXIMUM, -1);
obs_data_set_default_int(settings, KEY_RATECONTROL_QUALITY_TARGET, 0);
obs_data_set_default_int(settings, ST_RATECONTROL_QP_I, 21);
obs_data_set_default_int(settings, ST_RATECONTROL_QP_I_INITIAL, -1);
obs_data_set_default_int(settings, ST_RATECONTROL_QP_P, 21);
obs_data_set_default_int(settings, ST_RATECONTROL_QP_P_INITIAL, -1);
obs_data_set_default_int(settings, ST_RATECONTROL_QP_B, 21);
obs_data_set_default_int(settings, ST_RATECONTROL_QP_B_INITIAL, -1);
obs_data_set_default_int(settings, KEY_RATECONTROL_QP_I, 21);
obs_data_set_default_int(settings, KEY_RATECONTROL_QP_P, 21);
obs_data_set_default_int(settings, KEY_RATECONTROL_QP_B, 21);
obs_data_set_default_int(settings, ST_AQ_SPATIAL, -1);
obs_data_set_default_int(settings, ST_AQ_STRENGTH, 8);
obs_data_set_default_int(settings, ST_AQ_TEMPORAL, -1);
obs_data_set_default_int(settings, KEY_AQ_SPATIAL, -1);
obs_data_set_default_int(settings, KEY_AQ_STRENGTH, 8);
obs_data_set_default_int(settings, KEY_AQ_TEMPORAL, -1);
obs_data_set_default_int(settings, ST_OTHER_BFRAMES, 2);
obs_data_set_default_int(settings, ST_OTHER_BFRAME_REFERENCEMODE, static_cast<int64_t>(b_ref_mode::DISABLED));
obs_data_set_default_int(settings, ST_OTHER_ZEROLATENCY, -1);
obs_data_set_default_int(settings, ST_OTHER_WEIGHTED_PREDICTION, -1);
obs_data_set_default_int(settings, ST_OTHER_NONREFERENCE_PFRAMES, -1);
obs_data_set_default_int(settings, KEY_OTHER_BFRAMES, 2);
obs_data_set_default_int(settings, KEY_OTHER_BFRAMEREFERENCEMODE, static_cast<int64_t>(b_ref_mode::DISABLED));
obs_data_set_default_int(settings, KEY_OTHER_ZEROLATENCY, -1);
obs_data_set_default_int(settings, KEY_OTHER_WEIGHTEDPREDICTION, -1);
obs_data_set_default_int(settings, KEY_OTHER_NONREFERENCEPFRAMES, -1);
// Replay Buffer
obs_data_set_default_int(settings, "bitrate", 0);
@ -211,7 +224,6 @@ static bool modified_ratecontrol(obs_properties_t* props, obs_property_t*, obs_d
bool have_bitrate_max = false;
bool have_quality = false;
bool have_qp = false;
bool have_qp_init = false;
nvenc::ratecontrolmode rc = static_cast<nvenc::ratecontrolmode>(obs_data_get_int(settings, ST_RATECONTROL_MODE));
switch (rc) {
@ -229,27 +241,24 @@ static bool modified_ratecontrol(obs_properties_t* props, obs_property_t*, obs_d
have_bitrate = true;
have_bitrate_max = true;
have_quality = true;
have_qp_init = true;
have_qp = true;
break;
}
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_BITRATE), have_bitrate || have_bitrate_max);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_BITRATE_TARGET), have_bitrate);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_BITRATE_MAXIMUM), have_bitrate_max);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_BITRATE_TARGET), have_bitrate);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_BITRATE_MAXIMUM), have_bitrate_max);
obs_property_set_visible(obs_properties_get(props, S_RATECONTROL_BUFFERSIZE), have_bitrate || have_bitrate_max);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QUALITY), have_quality);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QUALITY_MINIMUM), have_quality);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QUALITY_MAXIMUM), have_quality);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QUALITY_TARGET), have_quality);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QUALITY), have_quality);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QUALITY_MINIMUM), have_quality);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QUALITY_MAXIMUM), have_quality);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QUALITY_TARGET), have_quality);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP), have_qp || have_qp_init);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_I), have_qp);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_P), have_qp);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_B), have_qp);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_I_INITIAL), have_qp_init);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_P_INITIAL), have_qp_init);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_B_INITIAL), have_qp_init);
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP), have_qp);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QP_I), have_qp);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QP_P), have_qp);
obs_property_set_visible(obs_properties_get(props, KEY_RATECONTROL_QP_B), have_qp);
return true;
}
@ -257,22 +266,22 @@ static bool modified_ratecontrol(obs_properties_t* props, obs_property_t*, obs_d
static bool modified_quality(obs_properties_t* props, obs_property_t*, obs_data_t* settings) noexcept
{
bool enabled = obs_data_get_bool(settings, ST_RATECONTROL_QUALITY);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QUALITY_MINIMUM), enabled);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QUALITY_MAXIMUM), enabled);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QUALITY_MINIMUM), enabled);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QUALITY_MAXIMUM), enabled);
return true;
}
static bool modified_aq(obs_properties_t* props, obs_property_t*, obs_data_t* settings) noexcept
{
bool spatial_aq = obs_data_get_int(settings, ST_AQ_SPATIAL) == 1;
obs_property_set_visible(obs_properties_get(props, ST_AQ_STRENGTH), spatial_aq);
bool spatial_aq = obs_data_get_int(settings, KEY_AQ_SPATIAL) == 1;
obs_property_set_visible(obs_properties_get(props, KEY_AQ_STRENGTH), spatial_aq);
return true;
}
void nvenc::get_properties_pre(obs_properties_t* props, const AVCodec*)
{
{
auto p = obs_properties_add_list(props, ST_PRESET, D_TRANSLATE(ST_PRESET), OBS_COMBO_TYPE_LIST,
auto p = obs_properties_add_list(props, KEY_PRESET, D_TRANSLATE(ST_PRESET), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_PRESET)));
for (auto kv : presets) {
@ -367,19 +376,19 @@ void nvenc::get_properties_post(obs_properties_t* props, const AVCodec* codec)
}
{
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QUALITY_MINIMUM,
auto p = obs_properties_add_int_slider(grp, KEY_RATECONTROL_QUALITY_MINIMUM,
D_TRANSLATE(ST_RATECONTROL_QUALITY_MINIMUM), 0, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QUALITY_MINIMUM)));
}
{
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QUALITY_MAXIMUM,
auto p = obs_properties_add_int_slider(grp, KEY_RATECONTROL_QUALITY_MAXIMUM,
D_TRANSLATE(ST_RATECONTROL_QUALITY_MAXIMUM), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QUALITY_MAXIMUM)));
}
}
{
auto p = obs_properties_add_float_slider(props, ST_RATECONTROL_QUALITY_TARGET,
auto p = obs_properties_add_float_slider(props, KEY_RATECONTROL_QUALITY_TARGET,
D_TRANSLATE(ST_RATECONTROL_QUALITY_TARGET), 0, 100, 0.01);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QUALITY_TARGET)));
}
@ -389,40 +398,25 @@ void nvenc::get_properties_post(obs_properties_t* props, const AVCodec* codec)
if (!util::are_property_groups_broken()) {
grp = obs_properties_create();
auto p = obs_properties_add_group(props, ST_RATECONTROL_QP, D_TRANSLATE(ST_RATECONTROL_QP),
OBS_GROUP_CHECKABLE, grp);
OBS_GROUP_NORMAL, grp);
obs_property_set_modified_callback(p, modified_quality);
}
{
auto p =
obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_I, D_TRANSLATE(ST_RATECONTROL_QP_I), 0, 51, 1);
obs_properties_add_int_slider(grp, KEY_RATECONTROL_QP_I, D_TRANSLATE(ST_RATECONTROL_QP_I), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QP_I)));
}
{
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_I_INITIAL,
D_TRANSLATE(ST_RATECONTROL_QP_I_INITIAL), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QP_I_INITIAL)));
}
{
auto p =
obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_P, D_TRANSLATE(ST_RATECONTROL_QP_P), 0, 51, 1);
obs_properties_add_int_slider(grp, KEY_RATECONTROL_QP_P, D_TRANSLATE(ST_RATECONTROL_QP_P), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QP_P)));
}
{
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_P_INITIAL,
D_TRANSLATE(ST_RATECONTROL_QP_P_INITIAL), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QP_P_INITIAL)));
}
{
auto p =
obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_B, D_TRANSLATE(ST_RATECONTROL_QP_B), 0, 51, 1);
obs_properties_add_int_slider(grp, KEY_RATECONTROL_QP_B, D_TRANSLATE(ST_RATECONTROL_QP_B), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QP_B)));
}
{
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_B_INITIAL,
D_TRANSLATE(ST_RATECONTROL_QP_B_INITIAL), -1, 51, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_RATECONTROL_QP_B_INITIAL)));
}
}
{
@ -433,16 +427,16 @@ void nvenc::get_properties_post(obs_properties_t* props, const AVCodec* codec)
}
{
auto p = util::obs_properties_add_tristate(grp, ST_AQ_SPATIAL, D_TRANSLATE(ST_AQ_SPATIAL));
auto p = util::obs_properties_add_tristate(grp, KEY_AQ_SPATIAL, D_TRANSLATE(ST_AQ_SPATIAL));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_AQ_SPATIAL)));
obs_property_set_modified_callback(p, modified_aq);
}
{
auto p = obs_properties_add_int_slider(grp, ST_AQ_STRENGTH, D_TRANSLATE(ST_AQ_STRENGTH), 1, 15, 1);
auto p = obs_properties_add_int_slider(grp, KEY_AQ_STRENGTH, D_TRANSLATE(ST_AQ_STRENGTH), 1, 15, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_AQ_STRENGTH)));
}
{
auto p = util::obs_properties_add_tristate(grp, ST_AQ_TEMPORAL, D_TRANSLATE(ST_AQ_TEMPORAL));
auto p = util::obs_properties_add_tristate(grp, KEY_AQ_TEMPORAL, D_TRANSLATE(ST_AQ_TEMPORAL));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_AQ_TEMPORAL)));
}
}
@ -455,80 +449,77 @@ void nvenc::get_properties_post(obs_properties_t* props, const AVCodec* codec)
}
{
auto p = obs_properties_add_int_slider(grp, ST_OTHER_BFRAMES, D_TRANSLATE(ST_OTHER_BFRAMES), 0, 4, 1);
auto p = obs_properties_add_int_slider(grp, KEY_OTHER_BFRAMES, D_TRANSLATE(ST_OTHER_BFRAMES), 0, 4, 1);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_BFRAMES)));
obs_property_int_set_suffix(p, " frames");
}
{
auto p =
obs_properties_add_list(grp, ST_OTHER_BFRAME_REFERENCEMODE, D_TRANSLATE(ST_OTHER_BFRAME_REFERENCEMODE),
obs_properties_add_list(grp, KEY_OTHER_BFRAMEREFERENCEMODE, D_TRANSLATE(ST_OTHER_BFRAMEREFERENCEMODE),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_BFRAME_REFERENCEMODE)));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_BFRAMEREFERENCEMODE)));
for (auto kv : b_ref_modes) {
obs_property_list_add_int(p, D_TRANSLATE(kv.second.c_str()), static_cast<int64_t>(kv.first));
}
}
{
auto p = util::obs_properties_add_tristate(grp, ST_OTHER_ZEROLATENCY, D_TRANSLATE(ST_OTHER_ZEROLATENCY));
auto p = util::obs_properties_add_tristate(grp, KEY_OTHER_ZEROLATENCY, D_TRANSLATE(ST_OTHER_ZEROLATENCY));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_ZEROLATENCY)));
}
{
auto p = util::obs_properties_add_tristate(grp, ST_OTHER_WEIGHTED_PREDICTION,
D_TRANSLATE(ST_OTHER_WEIGHTED_PREDICTION));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_WEIGHTED_PREDICTION)));
auto p = util::obs_properties_add_tristate(grp, KEY_OTHER_WEIGHTEDPREDICTION,
D_TRANSLATE(ST_OTHER_WEIGHTEDPREDICTION));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_WEIGHTEDPREDICTION)));
}
{
auto p = util::obs_properties_add_tristate(grp, ST_OTHER_NONREFERENCE_PFRAMES,
D_TRANSLATE(ST_OTHER_NONREFERENCE_PFRAMES));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_NONREFERENCE_PFRAMES)));
auto p = util::obs_properties_add_tristate(grp, KEY_OTHER_NONREFERENCEPFRAMES,
D_TRANSLATE(ST_OTHER_NONREFERENCEPFRAMES));
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_OTHER_NONREFERENCEPFRAMES)));
}
}
}
void nvenc::get_runtime_properties(obs_properties_t* props, const AVCodec*, AVCodecContext*)
{
obs_property_set_enabled(obs_properties_get(props, ST_PRESET), false);
obs_property_set_enabled(obs_properties_get(props, KEY_PRESET), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_MODE), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_TWOPASS), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_LOOKAHEAD), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_ADAPTIVEI), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_ADAPTIVEB), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_MODE), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_TWOPASS), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_LOOKAHEAD), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_ADAPTIVEI), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_ADAPTIVEB), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_BITRATE), true);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_BITRATE_TARGET), true);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_BITRATE_MAXIMUM), true);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_BITRATE_TARGET), true);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_BITRATE_MAXIMUM), true);
obs_property_set_enabled(obs_properties_get(props, S_RATECONTROL_BUFFERSIZE), true);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QUALITY), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QUALITY_MINIMUM), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QUALITY_MAXIMUM), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QUALITY_TARGET), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QUALITY_MINIMUM), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QUALITY_MAXIMUM), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QUALITY_TARGET), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_I), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_I_INITIAL), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_P), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_P_INITIAL), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_B), false);
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_B_INITIAL), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QP_I), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QP_P), false);
obs_property_set_enabled(obs_properties_get(props, KEY_RATECONTROL_QP_B), false);
obs_property_set_enabled(obs_properties_get(props, ST_AQ), false);
obs_property_set_enabled(obs_properties_get(props, ST_AQ_SPATIAL), false);
obs_property_set_enabled(obs_properties_get(props, ST_AQ_STRENGTH), false);
obs_property_set_enabled(obs_properties_get(props, ST_AQ_TEMPORAL), false);
obs_property_set_enabled(obs_properties_get(props, KEY_AQ_SPATIAL), false);
obs_property_set_enabled(obs_properties_get(props, KEY_AQ_STRENGTH), false);
obs_property_set_enabled(obs_properties_get(props, KEY_AQ_TEMPORAL), false);
obs_property_set_enabled(obs_properties_get(props, ST_OTHER), false);
obs_property_set_enabled(obs_properties_get(props, ST_OTHER_BFRAMES), false);
obs_property_set_enabled(obs_properties_get(props, ST_OTHER_BFRAME_REFERENCEMODE), false);
obs_property_set_enabled(obs_properties_get(props, ST_OTHER_ZEROLATENCY), false);
obs_property_set_enabled(obs_properties_get(props, ST_OTHER_WEIGHTED_PREDICTION), false);
obs_property_set_enabled(obs_properties_get(props, ST_OTHER_NONREFERENCE_PFRAMES), false);
obs_property_set_enabled(obs_properties_get(props, KEY_OTHER_BFRAMES), false);
obs_property_set_enabled(obs_properties_get(props, KEY_OTHER_BFRAMEREFERENCEMODE), false);
obs_property_set_enabled(obs_properties_get(props, KEY_OTHER_ZEROLATENCY), false);
obs_property_set_enabled(obs_properties_get(props, KEY_OTHER_WEIGHTEDPREDICTION), false);
obs_property_set_enabled(obs_properties_get(props, KEY_OTHER_NONREFERENCEPFRAMES), false);
}
void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
{
{
preset c_preset = static_cast<preset>(obs_data_get_int(settings, ST_PRESET));
preset c_preset = static_cast<preset>(obs_data_get_int(settings, KEY_PRESET));
auto found = preset_to_opt.find(c_preset);
if (found != preset_to_opt.end()) {
av_opt_set(context->priv_data, "preset", found->second.c_str(), 0);
@ -544,7 +535,7 @@ void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* c
bool have_qp = false;
bool have_qp_init = false;
ratecontrolmode rc = static_cast<ratecontrolmode>(obs_data_get_int(settings, ST_RATECONTROL_MODE));
ratecontrolmode rc = static_cast<ratecontrolmode>(obs_data_get_int(settings, KEY_RATECONTROL_MODE));
auto rcopt = ratecontrolmode_to_opt.find(rc);
if (rcopt != ratecontrolmode_to_opt.end()) {
av_opt_set(context->priv_data, "rc", rcopt->second.c_str(), 0);
@ -571,21 +562,21 @@ void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* c
break;
}
int tp = static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_TWOPASS));
int tp = static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_TWOPASS));
if (tp >= 0) {
av_opt_set_int(context->priv_data, "2pass", tp ? 1 : 0, 0);
}
int la = static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_LOOKAHEAD));
int la = static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_LOOKAHEAD));
av_opt_set_int(context->priv_data, "rc-lookahead", la, 0);
if (la > 0) {
int64_t adapt_i = obs_data_get_int(settings, ST_RATECONTROL_ADAPTIVEI);
int64_t adapt_i = obs_data_get_int(settings, KEY_RATECONTROL_ADAPTIVEI);
if (!util::is_tristate_default(adapt_i)) {
av_opt_set_int(context->priv_data, "no-scenecut", adapt_i, AV_OPT_SEARCH_CHILDREN);
}
if (strcmp(codec->name, "h264_nvenc")) {
int64_t adapt_b = obs_data_get_int(settings, ST_RATECONTROL_ADAPTIVEB);
int64_t adapt_b = obs_data_get_int(settings, KEY_RATECONTROL_ADAPTIVEB);
if (!util::is_tristate_default(adapt_b)) {
av_opt_set_int(context->priv_data, "b_adapt", adapt_b, AV_OPT_SEARCH_CHILDREN);
}
@ -593,25 +584,25 @@ void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* c
}
if (have_bitrate) {
context->bit_rate = static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_BITRATE_TARGET) * 1000);
context->bit_rate = static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_BITRATE_TARGET) * 1000);
// Support for Replay Buffer
obs_data_set_int(settings, "bitrate", obs_data_get_int(settings, ST_RATECONTROL_BITRATE_TARGET));
obs_data_set_int(settings, "bitrate", obs_data_get_int(settings, KEY_RATECONTROL_BITRATE_TARGET));
}
if (have_bitrate_max)
context->rc_max_rate = static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_BITRATE_MAXIMUM) * 1000);
context->rc_max_rate = static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_BITRATE_MAXIMUM) * 1000);
if (have_bitrate || have_bitrate_max)
context->rc_buffer_size = static_cast<int>(obs_data_get_int(settings, S_RATECONTROL_BUFFERSIZE) * 1000);
if (have_quality && obs_data_get_bool(settings, ST_RATECONTROL_QUALITY)) {
int qmin = static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_QUALITY_MINIMUM));
if (have_quality && obs_data_get_bool(settings, KEY_RATECONTROL_QUALITY)) {
int qmin = static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_QUALITY_MINIMUM));
context->qmin = qmin;
if (qmin >= 0) {
context->qmax = static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_QUALITY_MAXIMUM));
context->qmax = static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_QUALITY_MAXIMUM));
}
}
{
double_t v = obs_data_get_double(settings, ST_RATECONTROL_QUALITY_TARGET) / 100.0 * 51.0;
double_t v = obs_data_get_double(settings, KEY_RATECONTROL_QUALITY_TARGET) / 100.0 * 51.0;
if (v > 0) {
av_opt_set_double(context->priv_data, "cq", v, 0);
}
@ -619,22 +610,17 @@ void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* c
if (have_qp) {
av_opt_set_int(context->priv_data, "init_qpI",
static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_QP_I)), 0);
static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_QP_I)), 0);
av_opt_set_int(context->priv_data, "init_qpP",
static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_QP_P)), 0);
static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_QP_P)), 0);
av_opt_set_int(context->priv_data, "init_qpB",
static_cast<int>(obs_data_get_int(settings, ST_RATECONTROL_QP_B)), 0);
}
if (have_qp_init) {
av_opt_set_int(context->priv_data, "init_qpI", obs_data_get_int(settings, ST_RATECONTROL_QP_I_INITIAL), 0);
av_opt_set_int(context->priv_data, "init_qpP", obs_data_get_int(settings, ST_RATECONTROL_QP_P_INITIAL), 0);
av_opt_set_int(context->priv_data, "init_qpB", obs_data_get_int(settings, ST_RATECONTROL_QP_B_INITIAL), 0);
static_cast<int>(obs_data_get_int(settings, KEY_RATECONTROL_QP_B)), 0);
}
}
{ // AQ
int64_t saq = obs_data_get_int(settings, ST_AQ_SPATIAL);
int64_t taq = obs_data_get_int(settings, ST_AQ_TEMPORAL);
int64_t saq = obs_data_get_int(settings, KEY_AQ_SPATIAL);
int64_t taq = obs_data_get_int(settings, KEY_AQ_TEMPORAL);
if (strcmp(codec->name, "h264_nvenc") == 0) {
if (!util::is_tristate_default(saq))
@ -649,15 +635,15 @@ void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* c
}
if (util::is_tristate_enabled(saq))
av_opt_set_int(context->priv_data, "aq-strength",
static_cast<int>(obs_data_get_int(settings, ST_AQ_STRENGTH)), 0);
static_cast<int>(obs_data_get_int(settings, KEY_AQ_STRENGTH)), 0);
}
{ // Other
int64_t zl = obs_data_get_int(settings, ST_OTHER_ZEROLATENCY);
int64_t wp = obs_data_get_int(settings, ST_OTHER_WEIGHTED_PREDICTION);
int64_t nrp = obs_data_get_int(settings, ST_OTHER_NONREFERENCE_PFRAMES);
int64_t zl = obs_data_get_int(settings, KEY_OTHER_ZEROLATENCY);
int64_t wp = obs_data_get_int(settings, KEY_OTHER_WEIGHTEDPREDICTION);
int64_t nrp = obs_data_get_int(settings, KEY_OTHER_NONREFERENCEPFRAMES);
context->max_b_frames = static_cast<int>(obs_data_get_int(settings, ST_OTHER_BFRAMES));
context->max_b_frames = static_cast<int>(obs_data_get_int(settings, KEY_OTHER_BFRAMES));
if (!util::is_tristate_default(zl))
av_opt_set_int(context->priv_data, "zerolatency", zl, 0);
@ -673,7 +659,7 @@ void nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* c
{
auto found = b_ref_mode_to_opt.find(
static_cast<b_ref_mode>(obs_data_get_int(settings, ST_OTHER_BFRAME_REFERENCEMODE)));
static_cast<b_ref_mode>(obs_data_get_int(settings, KEY_OTHER_BFRAMEREFERENCEMODE)));
if (found != b_ref_mode_to_opt.end()) {
av_opt_set(context->priv_data, "b_ref_mode", found->second.c_str(), 0);
}

View file

@ -33,6 +33,14 @@ extern "C" {
#pragma warning(pop)
}
/* NVENC has multiple compression modes:
- CBR: Constant Bitrate (rc=cbr)
- VBR: Variable Bitrate (rc=vbr)
- CQP: Constant QP (rc=cqp)
- VQP: Variable QP (rc=vbr b=0 minrate=0 maxrate=0 qmin=minqp qmax=maxqp cq=targetqp)
- TQ: Target Quality (rc=vbr b=0 minrate=0 maxrate=0 qmin=qp qmax=qp cq=qp)
*/
using namespace encoder::ffmpeg;
namespace encoder::ffmpeg::handler::nvenc {