From 1ff21d661963c264fa46f94f17bb07b997ce682b Mon Sep 17 00:00:00 2001 From: trujimat-q Date: Tue, 25 Jun 2024 17:36:40 -0300 Subject: [PATCH 1/4] fixes for 1.2, CRF still not working --- src/ama-context.c | 10 +++++----- src/ama-context.h | 17 +++++++++++++---- src/ama-encoder.c | 35 ++++++++++++++++++----------------- src/ama-plugin-main.c | 2 +- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/ama-context.c b/src/ama-context.c index 1b0b8bc..b65bf67 100644 --- a/src/ama-context.c +++ b/src/ama-context.c @@ -101,20 +101,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 diff --git a/src/ama-context.h b/src/ama-context.h index b134a8f..924a339 100644 --- a/src/ama-context.h +++ b/src/ama-context.h @@ -24,8 +24,7 @@ #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 @@ -152,6 +151,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 @@ -160,7 +167,7 @@ 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_CRF_DISABLE -1 #define ENC_IDR_ENABLE 1 #define ENC_DEFAULT_FRAMERATE 30 #define ENC_DEFAULT_GOP_SIZE 30 @@ -235,6 +242,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; @@ -251,7 +260,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; diff --git a/src/ama-encoder.c b/src/ama-encoder.c index 40946f9..dc55659 100644 --- a/src/ama-encoder.c +++ b/src/ama-encoder.c @@ -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) @@ -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); diff --git a/src/ama-plugin-main.c b/src/ama-plugin-main.c index d6554f7..5bdb1fa 100644 --- a/src/ama-plugin-main.c +++ b/src/ama-plugin-main.c @@ -471,7 +471,7 @@ 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, "keyint_sec", TEXT_KEYINT_SEC, 0, 20, 1); From 96f2fbc751711974950461dbda1b6f6d3e783594 Mon Sep 17 00:00:00 2001 From: trujimat-q Date: Tue, 25 Jun 2024 17:40:21 -0300 Subject: [PATCH 2/4] fix formatting --- src/ama-context.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ama-context.h b/src/ama-context.h index 924a339..7e0e6aa 100644 --- a/src/ama-context.h +++ b/src/ama-context.h @@ -151,13 +151,13 @@ typedef enum HevcProfiles { ENC_HEVC_MAIN10_INTRA } HevcProfiles; -#define ENC_MIN_LATENCY_MS -1 -#define ENC_MAX_LATENCY_MS 60000 +#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 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 From f38c7b4f17c8cfaece3d2fb881ccabe70596f023 Mon Sep 17 00:00:00 2001 From: trujimat-q Date: Wed, 26 Jun 2024 17:51:12 -0300 Subject: [PATCH 3/4] changed crf management --- src/ama-context.c | 10 ++++------ src/ama-context.h | 6 +++--- src/ama-plugin-main.c | 19 ++++++++++++++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/ama-context.c b/src/ama-context.c index b65bf67..95d27ce 100644 --- a/src/ama-context.c +++ b/src/ama-context.c @@ -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 = @@ -129,10 +130,7 @@ AmaCtx *ama_create_context(obs_data_t *settings, obs_encoder_t *enc_handle, : ENC_PROFILE_DEFAULT; enc_props->level = ENC_DEFAULT_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; diff --git a/src/ama-context.h b/src/ama-context.h index 7e0e6aa..2317ca8 100644 --- a/src/ama-context.h +++ b/src/ama-context.h @@ -31,9 +31,10 @@ #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 @@ -167,7 +168,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 -1 #define ENC_IDR_ENABLE 1 #define ENC_DEFAULT_FRAMERATE 30 #define ENC_DEFAULT_GOP_SIZE 30 diff --git a/src/ama-plugin-main.c b/src/ama-plugin-main.c index 5bdb1fa..012ab41 100644 --- a/src/ama-plugin-main.c +++ b/src/ama-plugin-main.c @@ -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") @@ -228,15 +229,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; } @@ -317,6 +320,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); @@ -399,6 +405,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); @@ -473,6 +482,9 @@ static obs_properties_t *obs_ama_props_av1(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); + p = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20, 1); obs_property_int_set_suffix(p, " s"); @@ -507,6 +519,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", From fe4166332cd6c50d2f1533d547c0a91326a85ee4 Mon Sep 17 00:00:00 2001 From: trujimat-q Date: Thu, 27 Jun 2024 13:01:58 -0300 Subject: [PATCH 4/4] modified level according sdk 1.2 --- src/ama-context.c | 2 +- src/ama-context.h | 4 ++++ src/ama-plugin-main.c | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ama-context.c b/src/ama-context.c index 95d27ce..e0f614c 100644 --- a/src/ama-context.c +++ b/src/ama-context.c @@ -128,7 +128,7 @@ 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 = -1; enc_props->tune_metrics = 1; diff --git a/src/ama-context.h b/src/ama-context.h index 2317ca8..b5670ca 100644 --- a/src/ama-context.h +++ b/src/ama-context.h @@ -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 diff --git a/src/ama-plugin-main.c b/src/ama-plugin-main.c index 012ab41..e494be5 100644 --- a/src/ama-plugin-main.c +++ b/src/ama-plugin-main.c @@ -185,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); } @@ -343,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); @@ -427,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); @@ -485,6 +496,21 @@ static obs_properties_t *obs_ama_props_av1(void *unused) 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); obs_property_int_set_suffix(p, " s");