diff options
-rw-r--r-- | libhb/common.h | 2 | ||||
-rw-r--r-- | libhb/param.c | 75 | ||||
-rw-r--r-- | test/test.c | 19 |
3 files changed, 86 insertions, 10 deletions
diff --git a/libhb/common.h b/libhb/common.h index c4392fd94..77a5b9b31 100644 --- a/libhb/common.h +++ b/libhb/common.h @@ -1253,6 +1253,8 @@ hb_list_t *hb_filter_list_copy(const hb_list_t *src); void hb_filter_close( hb_filter_object_t ** ); char * hb_generate_filter_settings(int filter_id, const char *preset, const char *tune); +int hb_validate_filter_settings(int filter_id, const char *filter_param); +int hb_validate_param_string(const char *regex_pattern, const char *param_string); typedef void hb_error_handler_t( const char *errmsg ); diff --git a/libhb/param.c b/libhb/param.c index d95bf147f..96b1e2709 100644 --- a/libhb/param.c +++ b/libhb/param.c @@ -9,6 +9,7 @@ */ #include "hb.h" +#include <regex.h> /* NL-means presets and tunes * @@ -162,7 +163,7 @@ static char * generate_nlmeans_settings(const char *preset, const char *tune) } else { - hb_log("Unrecognized nlmeans tune (%s).\n", tune); + fprintf(stderr, "Unrecognized nlmeans tune (%s).\n", tune); return NULL; } @@ -179,7 +180,7 @@ static char * generate_nlmeans_settings(const char *preset, const char *tune) opt = strdup(preset); if (tune != NULL) { - hb_log("Custom nlmeans parameters specified; ignoring nlmeans tune (%s).\n", tune); + fprintf(stderr, "Custom nlmeans parameters specified; ignoring nlmeans tune (%s).\n", tune); } } @@ -196,6 +197,9 @@ static char * generate_nlmeans_settings(const char *preset, const char *tune) */ static char * generate_hqdn3d_settings(const char *preset, const char *tune) { + if (preset == NULL) + return NULL; + if (!strcasecmp(preset, "strong")) return strdup("7:7:7:5:5:5"); else if (!strcasecmp(preset, "medium")) @@ -208,19 +212,80 @@ static char * generate_hqdn3d_settings(const char *preset, const char *tune) return strdup(preset); } +int hb_validate_param_string(const char *regex_pattern, const char *param_string) +{ + regex_t regex_temp; + + if (regcomp(®ex_temp, regex_pattern, REG_EXTENDED) == 0) + { + if (regexec(®ex_temp, param_string, 0, NULL, 0) == 0) + { + regfree(®ex_temp); + return 0; + } + } + else + { + fprintf(stderr, "hb_validate_param_string: Error compiling regex for pattern (%s).\n", param_string); + } + + regfree(®ex_temp); + return 1; +} + +int hb_validate_filter_settings(int filter_id, const char *filter_param) +{ + // Regex matches "number" followed by one or more ":number", where number is uint or ufloat + const char *hb_colon_separated_params_regex = "^((([0-9]+([.][0-9]+)?)|([.][0-9]+))((:(([0-9]+([.][0-9]+)?)|([.][0-9]+)))+)?)$"; + + char *regex_pattern = NULL; + + switch (filter_id) + { + case HB_FILTER_NLMEANS: + case HB_FILTER_HQDN3D: + if (filter_param == NULL) + { + return 0; + } + regex_pattern = hb_colon_separated_params_regex; + break; + default: + fprintf(stderr, "hb_validate_filter_settings: Unrecognized filter (%d).\n", + filter_id); + return 1; + break; + } + + if (hb_validate_param_string(regex_pattern, filter_param) == 0) + { + return 0; + } + return 1; +} + char * hb_generate_filter_settings(int filter_id, const char *preset, const char *tune) { + char *filter_param = NULL; + switch (filter_id) { case HB_FILTER_NLMEANS: - return generate_nlmeans_settings(preset, tune); + filter_param = generate_nlmeans_settings(preset, tune); + break; case HB_FILTER_HQDN3D: - return generate_hqdn3d_settings(preset, tune); + filter_param = generate_hqdn3d_settings(preset, tune); + break; default: - hb_log("hb_generate_filter_settings: Unrecognized filter %d\n", + fprintf(stderr, "hb_generate_filter_settings: Unrecognized filter (%d).\n", filter_id); break; } + + if (hb_validate_filter_settings(filter_id, filter_param) == 0) + { + return filter_param; + } return NULL; } diff --git a/test/test.c b/test/test.c index a7299736c..bfc9294ff 100644 --- a/test/test.c +++ b/test/test.c @@ -4356,11 +4356,6 @@ static int ParseOptions( int argc, char ** argv ) if (nlmeans) { - if (nlmeans_opt == NULL && nlmeans_tune_opt != NULL) - { - fprintf(stdout, "Default nlmeans parameters specified; ignoring nlmeans tune (%s).\n", nlmeans_tune_opt); - } - char *opt = hb_generate_filter_settings(HB_FILTER_NLMEANS, nlmeans_opt, nlmeans_tune_opt); if (opt != NULL) @@ -4368,6 +4363,15 @@ static int ParseOptions( int argc, char ** argv ) free(nlmeans_opt); nlmeans_opt = opt; } + else if (nlmeans_opt != NULL) + { + fprintf(stderr, "Invalid parameters for nlmeans (%s).", nlmeans_opt); + return -1; + } + else if (nlmeans_tune_opt != NULL) + { + fprintf(stdout, "Default nlmeans parameters specified; ignoring nlmeans tune (%s).\n", nlmeans_tune_opt); + } } if (denoise) { @@ -4378,6 +4382,11 @@ static int ParseOptions( int argc, char ** argv ) free(denoise_opt); denoise_opt = opt; } + else if (denoise_opt != NULL) + { + fprintf(stderr, "Invalid parameters for hqdn3d (%s).", denoise_opt); + return -1; + } } return 0; |