From 8d965a1bfb3782343a03cff44977f11bb920f0b1 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 13 Mar 2020 16:49:39 +0100 Subject: options: change how option range min/max is handled Before this commit, option declarations used M_OPT_MIN/M_OPT_MAX (and some other identifiers based on these) to signal whether an option had min/max values. Remove these flags, and make it use a range implicitly on the condition if min. */ +#include #include #include #include @@ -786,17 +787,17 @@ void m_config_print_option_list(const struct m_config *config, const char *name) struct m_opt_choice_alternatives *alt = opt->priv; for (int n = 0; alt[n].name; n++) MP_INFO(config, " %s", alt[n].name); - if (opt->flags & (M_OPT_MIN | M_OPT_MAX)) + if (opt->min < opt->max) MP_INFO(config, " (or an integer)"); } else { MP_INFO(config, " %s", opt->type->name); } - if (opt->flags & (M_OPT_MIN | M_OPT_MAX)) { + if ((opt->type->flags & M_OPT_TYPE_USES_RANGE) && opt->min < opt->max) { snprintf(min, sizeof(min), "any"); snprintf(max, sizeof(max), "any"); - if (opt->flags & M_OPT_MIN) + if (opt->min != DBL_MIN) snprintf(min, sizeof(min), "%.14g", opt->min); - if (opt->flags & M_OPT_MAX) + if (opt->max != DBL_MAX) snprintf(max, sizeof(max), "%.14g", opt->max); MP_INFO(config, " (%s to %s)", min, max); } diff --git a/options/m_option.c b/options/m_option.c index 4bfecec44d..c31fececdf 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -52,6 +53,14 @@ const char m_option_path_separator = OPTION_PATH_SEPARATOR; +// For integer types: since min/max are floats and may not be able to represent +// the real min/max, and since opt.min/.max may use +/-INFINITY, some care has +// to be taken. (Also tricky rounding.) +#define OPT_INT_MIN(opt, T, Tm) \ + ((opt)->min < (opt)->max ? ((opt)->min <= (Tm) ? (Tm) : (T)((opt)->min)) : (Tm)) +#define OPT_INT_MAX(opt, T, Tm) \ + ((opt)->min < (opt)->max ? ((opt)->max >= (Tm) ? (Tm) : (T)((opt)->max)) : (Tm)) + char *m_option_strerror(int code) { switch (code) { @@ -193,16 +202,19 @@ const m_option_type_t m_option_type_flag = { #undef VAL -static int clamp_longlong(const m_option_t *opt, void *val) +static int clamp_longlong(const m_option_t *opt, long long i_min, long long i_max, + void *val) { long long v = *(long long *)val; int r = 0; - if ((opt->flags & M_OPT_MAX) && (v > opt->max)) { - v = opt->max; + long long min = OPT_INT_MIN(opt, long long, i_min); + long long max = OPT_INT_MAX(opt, long long, i_max); + if (v > max) { + v = max; r = M_OPT_OUT_OF_RANGE; } - if ((opt->flags & M_OPT_MIN) && (v < opt->min)) { - v = opt->min; + if (v < min) { + v = min; r = M_OPT_OUT_OF_RANGE; } *(long long *)val = v; @@ -210,6 +222,7 @@ static int clamp_longlong(const m_option_t *opt, void *val) } static int parse_longlong(struct mp_log *log, const m_option_t *opt, + long long i_min, long long i_max, struct bstr name, struct bstr param, void *dst) { if (param.len == 0) @@ -225,15 +238,17 @@ static int parse_longlong(struct mp_log *log, const m_option_t *opt, return M_OPT_INVALID; } - if ((opt->flags & M_OPT_MIN) && (tmp_int < opt->min)) { - mp_err(log, "The %.*s option must be >= %d: %.*s\n", - BSTR_P(name), (int) opt->min, BSTR_P(param)); + long long min = OPT_INT_MIN(opt, long long, i_min); + if (tmp_int < min) { + mp_err(log, "The %.*s option must be >= %lld: %.*s\n", + BSTR_P(name), min, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; } - if ((opt->flags & M_OPT_MAX) && (tmp_int > opt->max)) { - mp_err(log, "The %.*s option must be <= %d: %.*s\n", - BSTR_P(name), (int) opt->max, BSTR_P(param)); + long long max = OPT_INT_MAX(opt, long long, i_max); + if (tmp_int > max) { + mp_err(log, "The %.*s option must be <= %lld: %.*s\n", + BSTR_P(name), max, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; } @@ -246,7 +261,7 @@ static int parse_longlong(struct mp_log *log, const m_option_t *opt, static int clamp_int64(const m_option_t *opt, void *val) { long long tmp = *(int64_t *)val; - int r = clamp_longlong(opt, &tmp); + int r = clamp_longlong(opt, INT64_MIN, INT64_MAX, &tmp); *(int64_t *)val = tmp; return r; } @@ -255,7 +270,7 @@ static int parse_int(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst) { long long tmp; - int r = parse_longlong(log, opt, name, param, &tmp); + int r = parse_longlong(log, opt, INT_MIN, INT_MAX, name, param, &tmp); if (r >= 0 && dst) *(int *)dst = tmp; return r; @@ -265,7 +280,7 @@ static int parse_int64(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst) { long long tmp; - int r = parse_longlong(log, opt, name, param, &tmp); + int r = parse_longlong(log, opt, INT64_MIN, INT64_MAX, name, param, &tmp); if (r >= 0 && dst) *(int64_t *)dst = tmp; return r; @@ -290,8 +305,8 @@ static void add_int64(const m_option_t *opt, void *val, double add, bool wrap) int64_t nmin = is64 ? INT64_MIN : INT_MIN; int64_t nmax = is64 ? INT64_MAX : INT_MAX; - int64_t min = (opt->flags & M_OPT_MIN) ? opt->min : nmin; - int64_t max = (opt->flags & M_OPT_MAX) ? opt->max : nmax; + int64_t min = OPT_INT_MIN(opt, int64_t, nmin); + int64_t max = OPT_INT_MAX(opt, int64_t, nmax); if (v < min) v = wrap ? max : min; @@ -332,9 +347,9 @@ static int int64_set(const m_option_t *opt, void *dst, struct mpv_node *src) if (src->format != MPV_FORMAT_INT64) return M_OPT_UNKNOWN; int64_t val = src->u.int64; - if ((opt->flags & M_OPT_MIN) && val < opt->min) + if (val < OPT_INT_MIN(opt, int64_t, INT64_MIN)) return M_OPT_OUT_OF_RANGE; - if ((opt->flags & M_OPT_MAX) && val > opt->max) + if (val > OPT_INT_MAX(opt, int64_t, INT64_MAX)) return M_OPT_OUT_OF_RANGE; *(int64_t *)dst = val; return 1; @@ -380,6 +395,7 @@ static bool int64_equal(const m_option_t *opt, void *a, void *b) const m_option_type_t m_option_type_int = { .name = "Integer", + .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(int), .parse = parse_int, .print = print_int, @@ -393,6 +409,7 @@ const m_option_type_t m_option_type_int = { const m_option_type_t m_option_type_int64 = { .name = "Integer64", + .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(int64_t), .parse = parse_int64, .print = print_int, @@ -447,15 +464,20 @@ static int parse_byte_size(struct mp_log *log, const m_option_t *opt, tmp_int *= unit; - if ((opt->flags & M_OPT_MIN) && (tmp_int < opt->min)) { - mp_err(log, "The %.*s option must be >= %d: %.*s\n", - BSTR_P(name), (int) opt->min, BSTR_P(param)); + int64_t min = OPT_INT_MIN(opt, int64_t, INT64_MIN); + if (tmp_int < min) { + mp_err(log, "The %.*s option must be >= %"PRId64": %.*s\n", + BSTR_P(name), min, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; } - if ((opt->flags & M_OPT_MAX) && (tmp_int > opt->max)) { - mp_err(log, "The %.*s option must be <= %d: %.*s\n", - BSTR_P(name), (int) opt->max, BSTR_P(param)); + int64_t max = OPT_INT_MAX(opt, int64_t, INT64_MAX); + if (tmp_int > max) { + mp_err(log, "The %.*s option must be <= %"PRId64": %.*s\n", + BSTR_P(name), max, BSTR_P(param)); + double x = INT64_MAX; + printf("max: %ld, %f %f %ld %ld %d %d\n", max, x, opt->max, (int64_t)opt->max, (int64_t)x, x > INT64_MAX, x == INT64_MAX); + abort(); return M_OPT_OUT_OF_RANGE; } @@ -490,6 +512,7 @@ static char *pretty_print_byte_size(const m_option_t *opt, const void *val) const m_option_type_t m_option_type_byte_size = { .name = "ByteSize", + .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(int64_t), .parse = parse_byte_size, .print = print_int, @@ -517,7 +540,7 @@ static void print_choice_values(struct mp_log *log, const struct m_option *opt) struct m_opt_choice_alternatives *alt = opt->priv; for ( ; alt->name; alt++) mp_info(log, " %s\n", alt->name[0] ? alt->name : "(passing nothing)"); - if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) + if (opt->min < opt->max) mp_info(log, " %g-%g (integer range)\n", opt->min, opt->max); } @@ -544,9 +567,11 @@ static int parse_choice(struct mp_log *log, const struct m_option *opt, } if (param.len == 0) return M_OPT_MISSING_PARAM; - if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + if (opt->min < opt->max) { long long val; - if (parse_longlong(mp_null_log, opt, name, param, &val) == 1) { + if (parse_longlong(mp_null_log, opt, INT_MIN, INT_MAX, name, param, + &val) == 1) + { if (dst) *(int *)dst = val; return 1; @@ -573,7 +598,7 @@ static void choice_get_min_max(const struct m_option *opt, int *min, int *max) *min = MPMIN(*min, alt->value); *max = MPMAX(*max, alt->value); } - if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + if (opt->min < opt->max) { *min = MPMIN(*min, opt->min); *max = MPMAX(*max, opt->max); } @@ -600,7 +625,7 @@ static void add_choice(const m_option_t *opt, void *val, double add, bool wrap) if (fabs(add) < 0.5) return; - if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + if (opt->min < opt->max) { int newval = ival + add; if (ival >= opt->min && ival <= opt->max && newval >= opt->min && newval <= opt->max) @@ -655,7 +680,7 @@ static struct m_opt_choice_alternatives *get_choice(const m_option_t *opt, if (alt->value == v) return alt; } - if ((opt->flags & M_OPT_MIN) && (opt->flags & M_OPT_MAX)) { + if (opt->min < opt->max) { if (v >= opt->min && v <= opt->max) { *out_val = v; return NULL; @@ -708,7 +733,7 @@ static char *print_choice(const m_option_t *opt, const void *val) const struct m_option_type m_option_type_choice = { .name = "Choice", .size = sizeof(int), - .flags = M_OPT_TYPE_CHOICE, + .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, .parse = parse_choice, .print = print_choice, .copy = copy_opt, @@ -850,13 +875,15 @@ static int clamp_double(const m_option_t *opt, void *val) { double v = VAL(val); int r = 0; - if ((opt->flags & M_OPT_MAX) && (v > opt->max)) { - v = opt->max; - r = M_OPT_OUT_OF_RANGE; - } - if ((opt->flags & M_OPT_MIN) && (v < opt->min)) { - v = opt->min; - r = M_OPT_OUT_OF_RANGE; + if (opt->min < opt->max) { + if (v > opt->max) { + v = opt->max; + r = M_OPT_OUT_OF_RANGE; + } + if (v < opt->min) { + v = opt->min; + r = M_OPT_OUT_OF_RANGE; + } } // (setting max/min to INFINITY/-INFINITY is allowed) if (!isfinite(v) && v != opt->max && v != opt->min) { @@ -925,8 +952,8 @@ static void add_double(const m_option_t *opt, void *val, double add, bool wrap) v = v + add; - double min = (opt->flags & M_OPT_MIN) ? opt->min : -INFINITY; - double max = (opt->flags & M_OPT_MAX) ? opt->max : +INFINITY; + double min = opt->min < opt->max ? opt->min : -INFINITY; + double max = opt->min < opt->max ? opt->max : +INFINITY; if (v < min) v = wrap ? max : min; @@ -984,6 +1011,7 @@ static bool double_equal(const m_option_t *opt, void *a, void *b) const m_option_type_t m_option_type_double = { // double precision float or ratio (numerator[:/]denominator) .name = "Double", + .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(double), .parse = parse_double, .print = print_double, @@ -1059,6 +1087,7 @@ static bool float_equal(const m_option_t *opt, void *a, void *b) const m_option_type_t m_option_type_float = { // floating point number or ratio (numerator[:/]denominator) .name = "Float", + .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(float), .parse = parse_float, .print = print_float, @@ -1085,7 +1114,7 @@ static int parse_float_aspect(struct mp_log *log, const m_option_t *opt, const m_option_type_t m_option_type_aspect = { .name = "Aspect", .size = sizeof(float), - .flags = M_OPT_TYPE_CHOICE, + .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, .parse = parse_float_aspect, .print = print_float, .pretty_print = print_float_f3, diff --git a/options/m_option.h b/options/m_option.h index 9ee103bd94..8d7063c212 100644 --- a/options/m_option.h +++ b/options/m_option.h @@ -18,6 +18,7 @@ #ifndef MPLAYER_M_OPTION_H #define MPLAYER_M_OPTION_H +#include #include #include #include @@ -361,13 +362,14 @@ struct m_option { int offset; - // \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must - // also be set. - double min; - - // \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must - // also be set. - double max; + // Most numeric types restrict the range to [min, max] if min #include #include #include @@ -118,7 +119,7 @@ static const m_option_t mp_vo_opt_list[] = { OPT_SIZE_BOX("autofit", autofit, 0), OPT_SIZE_BOX("autofit-larger", autofit_larger, 0), OPT_SIZE_BOX("autofit-smaller", autofit_smaller, 0), - OPT_DOUBLE("window-scale", window_scale, CONF_RANGE, .min = 0.001, .max = 100), + OPT_DOUBLE("window-scale", window_scale, 0, .min = 0.001, .max = 100), OPT_FLAG("window-minimized", window_minimized, 0), OPT_FLAG("window-maximized", window_maximized, 0), OPT_FLAG("force-window-position", force_window_position, 0), @@ -149,7 +150,8 @@ static const m_option_t mp_vo_opt_list[] = { OPT_FLAG("keepaspect-window", keepaspect_window, 0), OPT_FLAG("hidpi-window-scale", hidpi_window_scale, 0), OPT_FLAG("native-fs", native_fs, 0), - OPT_DOUBLE("override-display-fps", override_display_fps, M_OPT_MIN, .min = 0), + OPT_DOUBLE("override-display-fps", override_display_fps, 0, + .min = 0, .max = DBL_MAX), OPT_DOUBLERANGE("video-timing-offset", timing_offset, 0, 0.0, 1.0), #if HAVE_X11 OPT_CHOICE("x11-netwm", x11_netwm, 0, @@ -454,7 +456,7 @@ static const m_option_t mp_opts[] = { {"always", 2})), OPT_FLAG("keep-open-pause", keep_open_pause, 0), OPT_DOUBLE("image-display-duration", image_display_duration, - M_OPT_RANGE, 0, INFINITY), + 0, 0, INFINITY), OPT_CHOICE("index", index_mode, 0, ({"default", 1}, {"recreate", 0})), @@ -498,7 +500,7 @@ static const m_option_t mp_opts[] = { OPT_FLAG("prefetch-playlist", prefetch_open, 0), OPT_FLAG("cache-pause", cache_pause, 0), OPT_FLAG("cache-pause-initial", cache_pause_initial, 0), - OPT_FLOAT("cache-pause-wait", cache_pause_wait, M_OPT_MIN, .min = 0), + OPT_FLOAT("cache-pause-wait", cache_pause_wait, 0, .min = 0, .max = DBL_MAX), OPT_DOUBLE("mf-fps", mf_fps, 0), OPT_STRING("mf-type", mf_type, 0), @@ -515,7 +517,7 @@ static const m_option_t mp_opts[] = { OPT_INTRANGE("audio-samplerate", force_srate, UPDATE_AUDIO, 0, 16*48000), OPT_CHANNELS("audio-channels", audio_output_channels, UPDATE_AUDIO), OPT_AUDIOFORMAT("audio-format", audio_output_format, UPDATE_AUDIO), - OPT_DOUBLE("speed", playback_speed, M_OPT_RANGE, .min = 0.01, .max = 100.0), + OPT_DOUBLE("speed", playback_speed, 0, .min = 0.01, .max = 100.0), OPT_FLAG("audio-pitch-correction", pitch_correction, 0), @@ -612,7 +614,7 @@ static const m_option_t mp_opts[] = { OPT_INTRANGE("osd-duration", osd_duration, 0, 0, 3600000), OPT_FLAG("osd-fractions", osd_fractions, 0), - OPT_DOUBLE("sstep", step_sec, CONF_MIN, 0), + OPT_DOUBLE("sstep", step_sec, 0, .min = 0, .max = DBL_MAX), OPT_CHOICE("framedrop", frame_dropping, 0, ({"no", 0}, @@ -665,12 +667,12 @@ static const m_option_t mp_opts[] = { {"display-vdrop", VS_DISP_VDROP}, {"display-desync", VS_DISP_NONE}, {"desync", VS_NONE})), - OPT_DOUBLE("video-sync-max-video-change", sync_max_video_change, - M_OPT_MIN, .min = 0), - OPT_DOUBLE("video-sync-max-audio-change", sync_max_audio_change, - M_OPT_MIN | M_OPT_MAX, .min = 0, .max = 1), - OPT_DOUBLE("video-sync-adrop-size", sync_audio_drop_size, - M_OPT_MIN | M_OPT_MAX, .min = 0, .max = 1), + OPT_DOUBLE("video-sync-max-video-change", sync_max_video_change, 0, + .min = 0, .max = DBL_MAX), + OPT_DOUBLE("video-sync-max-audio-change", sync_max_audio_change, 0, + .min = 0, .max = 1), + OPT_DOUBLE("video-sync-adrop-size", sync_audio_drop_size, 0, + .min = 0, .max = 1), OPT_CHOICE("hr-seek", hr_seek, 0, ({"no", -1}, {"absolute", 0}, {"yes", 1}, {"always", 1}, {"default", 2})), -- cgit v1.2.3