Signed-off-by: Nicolas George <[email protected]>
---
 libavfilter/vf_format.c | 47 ++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 19 deletions(-)

Because just
# [AVFilterGraph @ 0x55c09cfd19c0] Error initializing filters
# Error : Invalid argument
is not very helpful.

diff --git a/libavfilter/vf_format.c b/libavfilter/vf_format.c
index 76d64bb4ae..6ab7d9cbcc 100644
--- a/libavfilter/vf_format.c
+++ b/libavfilter/vf_format.c
@@ -88,25 +88,34 @@ static av_cold int invert_formats(AVFilterFormats **fmts,
     return 0;
 }
 
-static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void 
*log_ctx)
-{
-    char *tail;
-    int pix_fmt = av_get_pix_fmt(arg);
-    if (pix_fmt == AV_PIX_FMT_NONE) {
-        pix_fmt = strtol(arg, &tail, 0);
-        if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
-            av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
-            return AVERROR(EINVAL);
-        }
-    }
-    *ret = pix_fmt;
-    return 0;
+#define DEFINE_PARSE(name, Type, get, descr, extra_test) \
+static int parse_##name(enum Type *ret, const char *arg, void *log_ctx) \
+{ \
+    char *tail; \
+    int val = get(arg); \
+    if (val < 0) { \
+        val = strtol(arg, &tail, 0); \
+        if (*tail || extra_test) { \
+            av_log(log_ctx, AV_LOG_ERROR, "Invalid " descr " '%s'\n", arg); \
+            return AVERROR(EINVAL); \
+        } \
+    } \
+    *ret = val; \
+    return 0; \
 }
 
+DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", 
!av_pix_fmt_desc_get(val))
+DEFINE_PARSE(color_space, AVColorSpace, av_color_space_from_name, "color 
space", 0)
+DEFINE_PARSE(color_range, AVColorRange, av_color_range_from_name, "color 
range", 0)
+DEFINE_PARSE(alpha_mode, AVAlphaMode, av_alpha_mode_from_name, "alpha mode", 0)
+
 static av_cold int init(AVFilterContext *ctx)
 {
     FormatContext *s = ctx->priv;
     enum AVPixelFormat pix_fmt;
+    enum AVColorSpace csp;
+    enum AVColorRange crg;
+    enum AVAlphaMode alm;
     int ret;
 
     for (char *sep, *cur = s->pix_fmts; cur; cur = sep) {
@@ -122,8 +131,8 @@ static av_cold int init(AVFilterContext *ctx)
         sep = strchr(cur, '|');
         if (sep && *sep)
             *sep++ = 0;
-        if ((ret = av_color_space_from_name(cur)) < 0 ||
-            (ret = ff_add_format(&s->color_spaces, ret)) < 0)
+        if ((ret = parse_color_space(&csp, cur, ctx)) < 0 ||
+            (ret = ff_add_format(&s->color_spaces, csp)) < 0)
             return ret;
     }
 
@@ -131,8 +140,8 @@ static av_cold int init(AVFilterContext *ctx)
         sep = strchr(cur, '|');
         if (sep && *sep)
             *sep++ = 0;
-        if ((ret = av_color_range_from_name(cur)) < 0 ||
-            (ret = ff_add_format(&s->color_ranges, ret)) < 0)
+        if ((ret = parse_color_range(&crg, cur, ctx)) < 0 ||
+            (ret = ff_add_format(&s->color_ranges, crg)) < 0)
             return ret;
     }
 
@@ -140,8 +149,8 @@ static av_cold int init(AVFilterContext *ctx)
         sep = strchr(cur, '|');
         if (sep && *sep)
             *sep++ = 0;
-        if ((ret = av_alpha_mode_from_name(cur)) < 0 ||
-            (ret = ff_add_format(&s->alpha_modes, ret)) < 0)
+        if ((ret = parse_alpha_mode(&alm, cur, ctx)) < 0 ||
+            (ret = ff_add_format(&s->alpha_modes, alm)) < 0)
             return ret;
     }
 
-- 
2.53.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to