From 77f309c94ff17f5627290a7a5a4477db714ecd1e Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 25 Oct 2019 00:25:05 +0200 Subject: vo_gpu, options: don't return NaN through API Internally, vo_gpu uses NaN for some options to indicate a default value that is different depending on the context (e.g. different scalers). There are 2 problems with this: 1. you couldn't reset the options to their defaults 2. NaN is a damn mess and shouldn't be part of the API The option parser already rejected NaN explicitly, which is why 1. didn't work. Regarding 2., JSON might be a good example, and actually caused a bug report. Fix this by mapping NaN to the special value "default". I think I'd prefer other mechanisms (maybe just having every scaler expose separate options?), but for now this will do. See you in a future commit, which painfully deprecates this and replaces it with something else. I refrained from using "no" (my favorite magic value for "unset" etc.) because then I'd have e.g. make --no-scale-param1 work, which in addition to a lot of effort looks dumb and nobody will use it. Here's also an apology for the shitty added test script. Fixes: #6691 --- options/m_option.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'options/m_option.c') 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 = { -- cgit v1.2.3