diff mbox series

[FFmpeg-devel,32/47] fftools: return errors from parse_number_or_die() instead of aborting

Message ID 20230715104611.17902-32-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/47] fftools/ffmpeg_mux_init: handle pixel format endianness | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov July 15, 2023, 10:45 a.m. UTC
Rename the function to just parse_number().
---
 fftools/cmdutils.c        | 39 +++++++++++++++++++++++++++++----------
 fftools/cmdutils.h        |  6 ++----
 fftools/ffmpeg_mux_init.c |  7 +++++--
 fftools/ffmpeg_opt.c      | 23 ++++++++++++++++++-----
 fftools/ffplay.c          | 25 +++++++++++++++++++++----
 fftools/ffprobe.c         | 10 ++++++++--
 6 files changed, 83 insertions(+), 27 deletions(-)
diff mbox series

Patch

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index fb35245f0a..48a81ca201 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -104,8 +104,8 @@  void exit_program(int ret)
     exit(ret);
 }
 
-double parse_number_or_die(const char *context, const char *numstr, int type,
-                           double min, double max)
+int parse_number(const char *context, const char *numstr, int type,
+                           double min, double max, double *dst)
 {
     char *tail;
     const char *error;
@@ -118,11 +118,13 @@  double parse_number_or_die(const char *context, const char *numstr, int type,
         error = "Expected int64 for %s but found %s\n";
     else if (type == OPT_INT && (int)d != d)
         error = "Expected int for %s but found %s\n";
-    else
-        return d;
+    else {
+        *dst = d;
+        return 0;
+    }
+
     av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
-    exit_program(1);
-    return 0;
+    return AVERROR(EINVAL);
 }
 
 int64_t parse_time_or_die(const char *context, const char *timestr,
@@ -262,6 +264,7 @@  static int write_option(void *optctx, const OptionDef *po, const char *opt,
     void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
                 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
     int *dstcount;
+    double num;
     int ret;
 
     if (po->flags & OPT_SPEC) {
@@ -289,15 +292,31 @@  static int write_option(void *optctx, const OptionDef *po, const char *opt,
             return AVERROR(ENOMEM);
         *(char **)dst = str;
     } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
-        *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
+        ret = parse_number(opt, arg, OPT_INT64, INT_MIN, INT_MAX, &num);
+        if (ret < 0)
+            return ret;
+
+        *(int *)dst = num;
     } else if (po->flags & OPT_INT64) {
-        *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
+        ret = parse_number(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX, &num);
+        if (ret < 0)
+            return ret;
+
+        *(int64_t *)dst = num;
     } else if (po->flags & OPT_TIME) {
         *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
     } else if (po->flags & OPT_FLOAT) {
-        *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
+        ret = parse_number(opt, arg, OPT_FLOAT, -INFINITY, INFINITY, &num);
+        if (ret < 0)
+            return ret;
+
+        *(float *)dst = num;
     } else if (po->flags & OPT_DOUBLE) {
-        *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
+        ret = parse_number(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY, &num);
+        if (ret < 0)
+            return ret;
+
+        *(double *)dst = num;
     } else if (po->u.func_arg) {
         int ret = po->u.func_arg(optctx, opt, arg);
         if (ret < 0) {
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 69c2123804..68de0bcb7f 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -100,8 +100,6 @@  int opt_timelimit(void *optctx, const char *opt, const char *arg);
 
 /**
  * Parse a string and return its corresponding value as a double.
- * Exit from the application if the string cannot be correctly
- * parsed or the corresponding value is invalid.
  *
  * @param context the context of the value to be set (e.g. the
  * corresponding command line option name)
@@ -111,8 +109,8 @@  int opt_timelimit(void *optctx, const char *opt, const char *arg);
  * @param min the minimum valid accepted value
  * @param max the maximum valid accepted value
  */
-double parse_number_or_die(const char *context, const char *numstr, int type,
-                           double min, double max);
+int parse_number(const char *context, const char *numstr, int type,
+                           double min, double max, double *dst);
 
 /**
  * Parse a string specifying a time and return its corresponding
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index aebec0c573..02d71588ad 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -795,8 +795,11 @@  static int new_stream_video(Muxer *mux, const OptionsContext *o,
 
         ost->vsync_method = video_sync_method;
         MATCH_PER_STREAM_OPT(fps_mode, str, fps_mode, oc, st);
-        if (fps_mode)
-            parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file_index, ost->index, 0);
+        if (fps_mode) {
+            ret = parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file_index, ost->index, 0);
+            if (ret < 0)
+                return ret;
+        }
 
         if ((ost->frame_rate.num || ost->max_frame_rate.num) &&
             !(ost->vsync_method == VSYNC_AUTO ||
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 293397a8b7..e1696cdd59 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -194,7 +194,14 @@  int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_id
     }
 
     if (is_global && *vsync_var == VSYNC_AUTO) {
-        video_sync_method = parse_number_or_die("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR);
+        int ret;
+        double num;
+
+        ret = parse_number("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR, &num);
+        if (ret < 0)
+            return ret;
+
+        video_sync_method = num;
         av_log(NULL, AV_LOG_WARNING, "Passing a number to -vsync is deprecated,"
                " use a string argument as described in the manual.\n");
     }
@@ -1104,8 +1111,7 @@  static int opt_audio_filters(void *optctx, const char *opt, const char *arg)
 static int opt_vsync(void *optctx, const char *opt, const char *arg)
 {
     av_log(NULL, AV_LOG_WARNING, "-vsync is deprecated. Use -fps_mode\n");
-    parse_and_set_vsync(arg, &video_sync_method, -1, -1, 1);
-    return 0;
+    return parse_and_set_vsync(arg, &video_sync_method, -1, -1, 1);
 }
 
 static int opt_timecode(void *optctx, const char *opt, const char *arg)
@@ -1353,8 +1359,15 @@  static int opt_progress(void *optctx, const char *opt, const char *arg)
 int opt_timelimit(void *optctx, const char *opt, const char *arg)
 {
 #if HAVE_SETRLIMIT
-    int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
-    struct rlimit rl = { lim, lim + 1 };
+    int ret;
+    double lim;
+    struct rlimit rl;
+
+    ret = parse_number(opt, arg, OPT_INT64, 0, INT_MAX, &lim);
+    if (ret < 0)
+        return ret;
+
+    rl = (struct rlimit){ lim, lim + 1 };
     if (setrlimit(RLIMIT_CPU, &rl))
         perror("setrlimit");
 #else
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index 1f9757d607..6ca1ad98bf 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -3440,13 +3440,23 @@  static void event_loop(VideoState *cur_stream)
 
 static int opt_width(void *optctx, const char *opt, const char *arg)
 {
-    screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
+    double num;
+    int ret = parse_number(opt, arg, OPT_INT64, 1, INT_MAX, &num);
+    if (ret < 0)
+        return ret;
+
+    screen_width = num;
     return 0;
 }
 
 static int opt_height(void *optctx, const char *opt, const char *arg)
 {
-    screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
+    double num;
+    int ret = parse_number(opt, arg, OPT_INT64, 1, INT_MAX, &num);
+    if (ret < 0)
+        return ret;
+
+    screen_height = num;
     return 0;
 }
 
@@ -3491,8 +3501,15 @@  static int opt_show_mode(void *optctx, const char *opt, const char *arg)
 {
     show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
                 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
-                !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT  :
-                parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
+                !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT  : SHOW_MODE_NONE;
+
+    if (show_mode == SHOW_MODE_NONE) {
+        double num;
+        int ret = parse_number(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1, &num);
+        if (ret < 0)
+            return ret;
+        show_mode = num;
+    }
     return 0;
 }
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 91a3be660b..e6fd33492d 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -3665,8 +3665,14 @@  static int opt_show_optional_fields(void *optctx, const char *opt, const char *a
     else if (!av_strcasecmp(arg, "never"))  show_optional_fields = SHOW_OPTIONAL_FIELDS_NEVER;
     else if (!av_strcasecmp(arg, "auto"))   show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
 
-    if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto"))
-        show_optional_fields = parse_number_or_die("show_optional_fields", arg, OPT_INT, SHOW_OPTIONAL_FIELDS_AUTO, SHOW_OPTIONAL_FIELDS_ALWAYS);
+    if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto")) {
+        double num;
+        int ret = parse_number("show_optional_fields", arg, OPT_INT,
+                               SHOW_OPTIONAL_FIELDS_AUTO, SHOW_OPTIONAL_FIELDS_ALWAYS, &num);
+        if (ret < 0)
+            return ret;
+        show_optional_fields = num;
+    }
     return 0;
 }