diff options
Diffstat (limited to 'options')
-rw-r--r-- | options/m_option.c | 37 | ||||
-rw-r--r-- | options/m_option.h | 3 |
2 files changed, 31 insertions, 9 deletions
diff --git a/options/m_option.c b/options/m_option.c index 7bb43ad399..5df8b00291 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -918,6 +918,11 @@ static int parse_double(struct mp_log *log, const m_option_t *opt, if (bstr_eatstart0(&rest, ":") || bstr_eatstart0(&rest, "/")) tmp_float /= bstrtod(rest, &rest); + if ((opt->flags & M_OPT_DEFAULT_NAN) && bstr_equals0(param, "default")) { + tmp_float = NAN; + goto done; + } + if (rest.len) { mp_err(log, "The %.*s option must be a floating point number or a " "ratio (numerator[:/]denominator): %.*s\n", @@ -931,6 +936,7 @@ static int parse_double(struct mp_log *log, const m_option_t *opt, return M_OPT_OUT_OF_RANGE; } +done: if (dst) VAL(dst) = tmp_float; return 1; @@ -938,12 +944,18 @@ static int parse_double(struct mp_log *log, const m_option_t *opt, static char *print_double(const m_option_t *opt, const void *val) { - return talloc_asprintf(NULL, "%f", VAL(val)); + double f = VAL(val); + if (isnan(f) && (opt->flags & M_OPT_DEFAULT_NAN)) + return talloc_strdup(NULL, "default"); + return talloc_asprintf(NULL, "%f", f); } static char *print_double_f3(const m_option_t *opt, const void *val) { - return talloc_asprintf(NULL, "%.3f", VAL(val)); + double f = VAL(val); + if (isnan(f)) + return print_double(opt, val); + return talloc_asprintf(NULL, "%.3f", f); } static void add_double(const m_option_t *opt, void *val, double add, bool wrap) @@ -989,8 +1001,14 @@ static int double_set(const m_option_t *opt, void *dst, struct mpv_node *src) static int double_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src) { - dst->format = MPV_FORMAT_DOUBLE; - dst->u.double_ = *(double *)src; + double f = *(double *)src; + if (isnan(f) && (opt->flags & M_OPT_DEFAULT_NAN)) { + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(ta_parent, "default"); + } else { + dst->format = MPV_FORMAT_DOUBLE; + dst->u.double_ = f; + } return 1; } @@ -1023,12 +1041,14 @@ static int parse_float(struct mp_log *log, const m_option_t *opt, static char *print_float(const m_option_t *opt, const void *val) { - return talloc_asprintf(NULL, "%f", VAL(val)); + double tmp = VAL(val); + return print_double(opt, &tmp); } static char *print_float_f3(const m_option_t *opt, const void *val) { - return talloc_asprintf(NULL, "%.3f", VAL(val)); + double tmp = VAL(val); + return print_double_f3(opt, &tmp); } static void add_float(const m_option_t *opt, void *val, double add, bool wrap) @@ -1057,9 +1077,8 @@ static int float_set(const m_option_t *opt, void *dst, struct mpv_node *src) static int float_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src) { - dst->format = MPV_FORMAT_DOUBLE; - dst->u.double_ = VAL(src); - return 1; + double tmp = VAL(src); + return double_get(opt, ta_parent, dst, &tmp); } const m_option_type_t m_option_type_float = { diff --git a/options/m_option.h b/options/m_option.h index a3d008a400..390919141e 100644 --- a/options/m_option.h +++ b/options/m_option.h @@ -424,6 +424,9 @@ char *format_file_size(int64_t size); #define UPDATE_OPTS_MASK \ (((UPDATE_OPT_LAST << 1) - 1) & ~(unsigned)(UPDATE_OPT_FIRST - 1)) +// type_float/type_double: string "default" is parsed as NaN (and reverse) +#define M_OPT_DEFAULT_NAN (1 << 29) + // Like M_OPT_TYPE_OPTIONAL_PARAM. #define M_OPT_OPTIONAL_PARAM (1 << 30) |