Skip to content

Commit

Permalink
obs-ffmpeg: Add Pre-Analysis checkbox for AMD encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
rhutsAMD committed Oct 30, 2024
1 parent 57826be commit 1f354ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
2 changes: 2 additions & 0 deletions plugins/obs-ffmpeg/data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ FilePath="File Path"

AMFOpts="AMF/FFmpeg Options"
AMFOpts.ToolTip="Use to specify custom AMF or FFmpeg options. For example, \"level=5.2 profile=main\". Check the AMF encoder docs for more details."
AMF.PreAnalysis="Pre-Analysis"
AMF.PreAnalysis.ToolTip="Enables improved encoding quality at the cost of performance by calculating additional metrics such as activity and spatial complexity."

GPU="GPU"
BFrames="Max B-frames"
Expand Down
77 changes: 48 additions & 29 deletions plugins/obs-ffmpeg/texture-amf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,41 @@ static inline int64_t convert_to_obs_ts(amf_base *enc, int64_t ts)
return ts * (int64_t)enc->fps_den / amf_timebase;
}

static void convert_to_encoder_packet(amf_base *enc, AMFDataPtr &data, encoder_packet *packet)
static inline const char *
convert_amf_memory_type_to_string(AMF_MEMORY_TYPE type)
{
switch (type) {
case AMF_MEMORY_UNKNOWN:
return "Unknown";
case AMF_MEMORY_HOST:
return "Host";
case AMF_MEMORY_DX9:
return "DX9";
case AMF_MEMORY_DX11:
return "DX11";
case AMF_MEMORY_OPENCL:
return "OpenCL";
case AMF_MEMORY_OPENGL:
return "OpenGL";
case AMF_MEMORY_XV:
return "XV";
case AMF_MEMORY_GRALLOC:
return "Gralloc";
case AMF_MEMORY_COMPUTE_FOR_DX9:
return "Compute For DX9";
case AMF_MEMORY_COMPUTE_FOR_DX11:
return "Compute For DX11";
case AMF_MEMORY_VULKAN:
return "Vulkan";
case AMF_MEMORY_DX12:
return "DX12";
default:
return "Invalid";
}
}

static void convert_to_encoder_packet(amf_base *enc, AMFDataPtr &data,
encoder_packet *packet)
{
if (!data)
return;
Expand Down Expand Up @@ -1082,10 +1116,11 @@ static void check_texture_encode_capability(obs_encoder_t *encoder, amf_codec_ty

#include "texture-amf-opts.hpp"

/* These are initial recommended settings that may be lowered later
* once we know more info such as the resolution and frame rate.
*/
static void amf_avc_defaults(obs_data_t *settings)
{
// These are initial recommended settings that may be lowered later
// once we know more info such as the resolution and frame rate.
obs_data_set_default_string(settings, "rate_control", "CBR");
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "cqp", 20);
Expand All @@ -1094,27 +1129,8 @@ static void amf_avc_defaults(obs_data_t *settings)
obs_data_set_default_int(settings, "bf", 2);
}

static void amf_hevc_defaults(obs_data_t *settings)
{
// These are initial recommended settings that may be lowered later
// once we know more info such as the resolution and frame rate.
obs_data_set_default_string(settings, "rate_control", "VBR");
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "cqp", 20);
obs_data_set_default_string(settings, "preset", "quality");
}

static void amf_av1_defaults(obs_data_t *settings)
{
// These are initial recommended settings that may be lowered later
// once we know more info such as the resolution and frame rate.
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "cqp", 20);
obs_data_set_default_string(settings, "rate_control", "VBR");
obs_data_set_default_string(settings, "preset", "highQuality");
}

static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p, obs_data_t *settings)
static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings)
{
const char *rc = obs_data_get_string(settings, "rate_control");
bool cqp = astrcmpi(rc, "CQP") == 0;
Expand Down Expand Up @@ -1178,6 +1194,11 @@ static obs_properties_t *amf_properties_internal(amf_codec_type codec)
#undef add_profile
}

p = obs_properties_add_bool(props, "pre_analysis",
obs_module_text("AMF.PreAnalysis"));
obs_property_set_long_description(
p, obs_module_text("AMF.PreAnalysis.ToolTip"));

if (amf_codec_type::AVC == codec) {
obs_properties_add_int(props, "bf", obs_module_text("BFrames"), 0, 5, 1);
}
Expand Down Expand Up @@ -1329,7 +1350,6 @@ static inline void adjust_recommended_avc_defaults(amf_base *enc,
{
int64_t framerate = enc->fps_num / enc->fps_den;
if ((enc->cx * enc->cy > 1920 * 1088) || (framerate > 60)) {

// Recommended base defaults
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "cqp", 20);
Expand All @@ -1356,6 +1376,7 @@ static bool amf_avc_init(void *data, obs_data_t *settings)
const char *profile = obs_data_get_string(settings, "profile");
const char *rc_str = obs_data_get_string(settings, "rate_control");
int64_t bf = obs_data_get_int(settings, "bf");
bool pa_enabled = obs_data_get_bool(settings, "pre_analysis");

if (enc->bframes_supported) {
set_avc_property(enc, MAX_CONSECUTIVE_BPICTURES, bf);
Expand All @@ -1365,7 +1386,7 @@ static bool amf_avc_init(void *data, obs_data_t *settings)
* as those with high motion. This only takes effect if
* Pre-Analysis is enabled.
*/
if (bf > 0) {
if (bf > 0 && pa_enabled == true) {
set_avc_property(enc, ADAPTIVE_MINIGOP, true);
}

Expand Down Expand Up @@ -1689,7 +1710,6 @@ static inline void adjust_recommended_hevc_defaults(amf_base *enc,
const bool is10bit = enc->amf_format == AMF_SURFACE_P010;
const int64_t framerate = enc->fps_num / enc->fps_den;
if ((enc->cx * enc->cy > 1920 * 1088) || is10bit || (framerate > 60)) {

// Recommended base defaults
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "cqp", 20);
Expand Down Expand Up @@ -2077,7 +2097,6 @@ static inline void adjust_recommended_av1_defaults(amf_base *enc,
const bool is10bit = enc->amf_format == AMF_SURFACE_P010;
const int64_t framerate = enc->fps_num / enc->fps_den;
if ((enc->cx * enc->cy > 1920 * 1088) || is10bit || (framerate > 60)) {

// Recommended base defaults
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "cqp", 20);
Expand Down Expand Up @@ -2125,7 +2144,7 @@ static bool amf_av1_init(void *data, obs_data_t *settings)
if (!ffmpeg_opts || !*ffmpeg_opts)
ffmpeg_opts = "(none)";

info("Settings:\n"
info("settings:\n"
"\trate_control: %s\n"
"\tbitrate: %d\n"
"\tcqp: %d\n"
Expand Down

0 comments on commit 1f354ea

Please sign in to comment.