Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/ama-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ AmaCtx *ama_create_context(obs_data_t *settings, obs_encoder_t *enc_handle,
control_rate == ENC_RC_MODE_CABR
? (int)obs_data_get_int(custom_settings, "max_bitrate")
: ENC_DEFAULT_MAX_BITRATE;
enc_props->crf = control_rate == ENC_CRF_ENABLE_ALIAS ? ENC_CRF_ENABLE
: ENC_CRF_DISABLE;
enc_props->crf = control_rate == ENC_CRF_ENABLE_ALIAS
? (int)obs_data_get_int(custom_settings, "crf")
: ENC_CRF_DEFAULT;
enc_props->force_idr = ENC_IDR_ENABLE;
enc_props->fps = voi->fps_num / voi->fps_den;
enc_props->gop_size =
Expand All @@ -101,20 +102,20 @@ AmaCtx *ama_create_context(obs_data_t *settings, obs_encoder_t *enc_handle,
"keyint_sec")
: enc_props->fps * 2;
enc_props->min_qp = ENC_DEFAULT_MIN_QP;
enc_props->max_qp = ctx->codec == ENCODER_ID_AV1
? ENC_SUPPORTED_MAX_AV1_QP
: ENC_DEFAULT_MAX_QP;
enc_props->max_qp = ctx->codec == ENCODER_ID_AV1 ? ENC_SUPPORTED_MAX_QP
: ENC_DEFAULT_MAX_QP;
enc_props->num_bframes =
obs_data_get_bool(custom_settings, "lookahead")
? (int)obs_data_get_int(custom_settings, "b_frames")
: ENC_MIN_NUM_B_FRAMES;
enc_props->spat_aq_gain = ENC_AQ_GAIN_NOT_USED;
enc_props->temp_aq_gain = ENC_AQ_GAIN_NOT_USED;
enc_props->latency_ms = ENC_DEFAULT_LATENCY_MS;
enc_props->bufsize = ENC_DEFAULT_BUFSIZE;
enc_props->spatial_aq = ENC_DEFAULT_SPATIAL_AQ;
enc_props->temporal_aq = ENC_DEFAULT_TEMPORAL_AQ;
enc_props->slice = DEFAULT_SLICE_ID;
enc_props->qp = (control_rate == ENC_CRF_ENABLE_ALIAS ||
control_rate == ENC_RC_MODE_CONSTANT_QP)
enc_props->qp = (control_rate == ENC_RC_MODE_CONSTANT_QP)
? (int)obs_data_get_int(custom_settings, "qp")
: ENC_DEFAULT_MIN_QP;
enc_props->rc_mode = control_rate != ENC_CRF_ENABLE_ALIAS
Expand All @@ -127,12 +128,9 @@ AmaCtx *ama_create_context(obs_data_t *settings, obs_encoder_t *enc_handle,
ctx->codec != ENCODER_ID_AV1
? (int)obs_data_get_int(custom_settings, "profile")
: ENC_PROFILE_DEFAULT;
enc_props->level = ENC_DEFAULT_LEVEL;
enc_props->level = (int)obs_data_get_int(custom_settings, "level");
enc_props->tier = -1;
enc_props->lookahead_depth =
obs_data_get_bool(custom_settings, "lookahead")
? ENC_DEFAULT_LOOKAHEAD_DEPTH
: 0;
enc_props->lookahead_depth = -1;
enc_props->tune_metrics = 1;
enc_props->dynamic_gop = -1;
enc_props->pix_fmt = XMA_YUV420P_FMT_TYPE;
Expand Down
25 changes: 19 additions & 6 deletions src/ama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
#define ENC_MIN_GOP_SIZE -1
#define ENC_MAX_GOP_SIZE 1000
#define ENC_SUPPORTED_MIN_QP -1
#define ENC_SUPPORTED_MAX_QP 51
#define ENC_SUPPORTED_MAX_AV1_QP 255
#define ENC_SUPPORTED_MAX_QP 63
#define ENC_SPATIAL_AQ_DISABLE 0
#define ENC_SPATIAL_AQ_ENABLE 1
#define ENC_TEMPORAL_AQ_DISABLE 0
#define ENC_TEMPORAL_AQ_ENABLE 1
#define ENC_DEFAULT_TUNE_METRICS 1
#define ENC_MAX_TUNE_METRICS 4
#define ENC_CRF_ENABLE 1
#define ENC_CRF_ENABLE_ALIAS 255
#define ENC_CRF_DEFAULT ENC_CRF_DISABLE
#define ENC_MIN_CRF -1
#define ENC_MAX_CRF 63
#define ENC_CRF_DEFAULT -1
#define ENC_IDR_DISABLE 0

#define ENC_MIN_LOOKAHEAD_DEPTH -1
Expand Down Expand Up @@ -83,6 +83,10 @@
#define ENC_LEVEL_51 51
#define ENC_LEVEL_52 52
#define ENC_LEVEL_53 53
#define ENC_LEVEL_60 60
#define ENC_LEVEL_61 61
#define ENC_LEVEL_62 62
#define ENC_LEVEL_63 63

#define ENC_PROFILE_AUTO -1
#define ENC_PROFILE_DEFAULT ENC_PROFILE_AUTO
Expand Down Expand Up @@ -152,6 +156,14 @@ typedef enum HevcProfiles {
ENC_HEVC_MAIN10_INTRA
} HevcProfiles;

#define ENC_MIN_LATENCY_MS -1
#define ENC_MAX_LATENCY_MS 60000
#define ENC_DEFAULT_LATENCY_MS ENC_MIN_LATENCY_MS

#define ENC_MIN_BUFSIZE XMA_ENC_BUFSIZE_MIN
#define ENC_MAX_BUFSIZE XMA_ENC_BUFSIZE_MAX
#define ENC_DEFAULT_BUFSIZE XMA_ENC_BUFSIZE_DEFAULT

#define DEFAULT_DEVICE_ID 0
#define DEFAULT_SLICE_ID -1

Expand All @@ -160,7 +172,6 @@ typedef enum HevcProfiles {
#define ENC_SUPPORTED_MAX_BITRATE 3500000
#define ENC_DEFAULT_BITRATE 2500
#define ENC_DEFAULT_MAX_BITRATE ENC_SUPPORTED_MIN_BITRATE
#define ENC_CRF_DISABLE 0
#define ENC_IDR_ENABLE 1
#define ENC_DEFAULT_FRAMERATE 30
#define ENC_DEFAULT_GOP_SIZE 30
Expand Down Expand Up @@ -235,6 +246,8 @@ typedef struct {
int32_t tune_metrics;
int32_t dynamic_gop;
int32_t cores;
int32_t bufsize;
int32_t latency_ms;
char expert_options[2048];
int32_t latency_logging;
} EncoderProperties;
Expand All @@ -251,7 +264,7 @@ typedef struct {
size_t num_frames_received;
XmaFilterSession *upload_session;
XmaEncoderSession *enc_session;
XrmEncodeContext xrm_enc_ctx;
XrmEncodeContextV2 xrm_enc_ctx;
EncoderProperties enc_props;
DynIdrFrames *dynamic_idr;
uint8_t *header_data;
Expand Down
35 changes: 18 additions & 17 deletions src/ama-encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,31 @@ void enc_free_xma_props(XmaEncoderProperties *xma_enc_props)
int32_t encoder_reserve(AmaCtx *ctx)
{
da_init(ctx->dts_array);
XrmInterfaceProperties xrm_props;
memset(&xrm_props, 0, sizeof(xrm_props));

xrm_props.width = ctx->enc_props.width;
xrm_props.height = ctx->enc_props.height;
xrm_props.fps_num = ctx->enc_props.fps;
xrm_props.fps_den = 1;
xrm_props.enc_cores = ctx->enc_props.cores;
XrmEncodePropsV2 *xrm_props =
(XrmEncodePropsV2 *)xrm_props_create(XRM_IP_ENCODER);

xrm_props->input.width = ctx->enc_props.width;
xrm_props->input.height = ctx->enc_props.height;
xrm_props->input.fps_num = ctx->enc_props.fps;
xrm_props->input.fps_den = 1;
xrm_props->is_la_enabled = ctx->enc_props.lookahead_depth != 0;
xrm_props->enc_cores = ctx->enc_props.cores;
switch (ctx->enc_props.preset) {
case XMA_ENC_PRESET_SLOW:
strcpy(xrm_props.preset, "slow");
strcpy(xrm_props->preset, "slow");
case XMA_ENC_PRESET_MEDIUM:
strcpy(xrm_props.preset, "medium");
strcpy(xrm_props->preset, "medium");
case XMA_ENC_PRESET_FAST:
strcpy(xrm_props.preset, "fast");
strcpy(xrm_props->preset, "fast");
default:
strcpy(xrm_props.preset, "medium");
strcpy(xrm_props->preset, "medium");
}
xrm_props.is_la_enabled = ctx->enc_props.lookahead_depth != 0;
bool isAV1 = ctx->codec == ENCODER_ID_AV1;
xrm_props->is_av1_type1 = isAV1;
xrm_props->slice_id = ctx->enc_props.slice;
xrm_props->dev_index = ctx->enc_props.device_id;
ctx->xrm_enc_ctx.slice_id = ctx->enc_props.slice;

return xrm_enc_reserve(&ctx->xrm_enc_ctx, ctx->enc_props.device_id,
ctx->enc_props.slice, isAV1, false, &xrm_props);
return xrm_enc_reserve_v2(&ctx->xrm_enc_ctx, xrm_props);
}

int32_t encoder_create(AmaCtx *ctx)
Expand Down Expand Up @@ -361,7 +362,7 @@ int32_t encoder_destroy(AmaCtx *ctx)
xma_enc_session_destroy(ctx->enc_session);
ctx->enc_session = NULL;
}
xrm_enc_release(&ctx->xrm_enc_ctx);
xrm_enc_release_v2(&ctx->xrm_enc_ctx);

enc_free_xma_props(&ctx->xma_enc_props);

Expand Down
47 changes: 43 additions & 4 deletions src/ama-plugin-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OBS_MODULE_USE_DEFAULT_LOCALE(PLUGIN_NAME, "en-US")
#define TEXT_BITRATE obs_module_text("Bitrate")
#define TEXT_MAX_BITRATE obs_module_text("Max Bitrate")
#define TEXT_QP obs_module_text("QP")
#define TEXT_CRF obs_module_text("CRF")
#define TEXT_B_FRAMES obs_module_text("B Frames")
#define TEXT_PROFILE obs_module_text("Profile")
#define TEXT_LEVEL obs_module_text("Level")
Expand Down Expand Up @@ -184,6 +185,9 @@ void *ama_create_hevc(obs_data_t *settings, obs_encoder_t *encoder)
void *ama_create_av1(obs_data_t *settings, obs_encoder_t *encoder)
{
obs_log(LOG_INFO, "ama_create_av1");
if (!ama_validate_encoding_level(settings, encoder)) {
return NULL;
}
return ama_create(settings, encoder, ENCODER_ID_AV1);
}

Expand Down Expand Up @@ -228,15 +232,17 @@ static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
int rc = (int)obs_data_get_int(settings, "control_rate");
bool cabr = rc == ENC_RC_MODE_CABR;
bool cabr_or_cbr = rc == ENC_RC_MODE_CABR || rc == ENC_RC_MODE_CBR;
bool cqp_or_crf = rc == ENC_RC_MODE_CONSTANT_QP ||
rc == ENC_CRF_ENABLE_ALIAS;
bool cqp = rc == ENC_RC_MODE_CONSTANT_QP;
bool crf = rc == ENC_CRF_ENABLE_ALIAS;

p = obs_properties_get(ppts, "bitrate");
obs_property_set_visible(p, cabr_or_cbr);
p = obs_properties_get(ppts, "max_bitrate");
obs_property_set_visible(p, cabr);
p = obs_properties_get(ppts, "qp");
obs_property_set_visible(p, cqp_or_crf);
obs_property_set_visible(p, cqp);
p = obs_properties_get(ppts, "crf");
obs_property_set_visible(p, crf);

return true;
}
Expand Down Expand Up @@ -317,6 +323,9 @@ static obs_properties_t *obs_ama_props_h264(void *unused)
p = obs_properties_add_int(props, "qp", TEXT_QP, ENC_SUPPORTED_MIN_QP,
ENC_SUPPORTED_MAX_QP, 1);

p = obs_properties_add_int(props, "crf", TEXT_CRF, ENC_MIN_CRF,
ENC_MAX_CRF, 1);

list = obs_properties_add_list(props, "profile", TEXT_PROFILE,
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
Expand All @@ -337,6 +346,10 @@ static obs_properties_t *obs_ama_props_h264(void *unused)
obs_property_list_add_int(list, "5", ENC_LEVEL_50);
obs_property_list_add_int(list, "5.1", ENC_LEVEL_51);
obs_property_list_add_int(list, "5.2", ENC_LEVEL_52);
obs_property_list_add_int(list, "5.3", ENC_LEVEL_53);
obs_property_list_add_int(list, "6.0", ENC_LEVEL_60);
obs_property_list_add_int(list, "6.1", ENC_LEVEL_61);
obs_property_list_add_int(list, "6.2", ENC_LEVEL_62);

p = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20,
1);
Expand Down Expand Up @@ -399,6 +412,9 @@ static obs_properties_t *obs_ama_props_hevc(void *unused)
p = obs_properties_add_int(props, "qp", TEXT_QP, ENC_SUPPORTED_MIN_QP,
ENC_SUPPORTED_MAX_QP, 1);

p = obs_properties_add_int(props, "crf", TEXT_CRF, ENC_MIN_CRF,
ENC_MAX_CRF, 1);

list = obs_properties_add_list(props, "profile", TEXT_PROFILE,
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
Expand All @@ -418,6 +434,10 @@ static obs_properties_t *obs_ama_props_hevc(void *unused)
obs_property_list_add_int(list, "5", ENC_LEVEL_50);
obs_property_list_add_int(list, "5.1", ENC_LEVEL_51);
obs_property_list_add_int(list, "5.2", ENC_LEVEL_52);
obs_property_list_add_int(list, "5.3", ENC_LEVEL_53);
obs_property_list_add_int(list, "6.0", ENC_LEVEL_60);
obs_property_list_add_int(list, "6.1", ENC_LEVEL_61);
obs_property_list_add_int(list, "6.2", ENC_LEVEL_62);

p = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20,
1);
Expand Down Expand Up @@ -471,7 +491,25 @@ static obs_properties_t *obs_ama_props_av1(void *unused)
obs_property_int_set_suffix(p, " Kbps");

p = obs_properties_add_int(props, "qp", TEXT_QP, ENC_SUPPORTED_MIN_QP,
ENC_SUPPORTED_MAX_AV1_QP, 1);
ENC_SUPPORTED_MAX_QP, 1);

p = obs_properties_add_int(props, "crf", TEXT_CRF, ENC_MIN_CRF,
ENC_MAX_CRF, 1);

list = obs_properties_add_list(props, "level", TEXT_LEVEL,
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(list, "3.1", ENC_LEVEL_31);
obs_property_list_add_int(list, "4", ENC_LEVEL_40);
obs_property_list_add_int(list, "4.1", ENC_LEVEL_41);
obs_property_list_add_int(list, "5", ENC_LEVEL_50);
obs_property_list_add_int(list, "5.1", ENC_LEVEL_51);
obs_property_list_add_int(list, "5.2", ENC_LEVEL_52);
obs_property_list_add_int(list, "5.3", ENC_LEVEL_53);
obs_property_list_add_int(list, "6.0", ENC_LEVEL_60);
obs_property_list_add_int(list, "6.1", ENC_LEVEL_61);
obs_property_list_add_int(list, "6.2", ENC_LEVEL_62);
obs_property_list_add_int(list, "6.3", ENC_LEVEL_63);

p = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20,
1);
Expand Down Expand Up @@ -507,6 +545,7 @@ static void obs_ama_defaults(obs_data_t *settings)
obs_data_set_default_int(settings, "control_rate", ENC_RC_MODE_CBR);
obs_data_set_default_int(settings, "level", ENC_DEFAULT_LEVEL);
obs_data_set_default_int(settings, "qp", ENC_DEFAULT_QP);
obs_data_set_default_int(settings, "crf", ENC_CRF_DEFAULT);
obs_data_set_default_int(settings, "profile", ENC_PROFILE_DEFAULT);
obs_data_set_default_bool(settings, "enable_scaling", false);
obs_data_set_default_int(settings, "scaler_resolution",
Expand Down