[PATCH 1/2] lavfi/vf_format: report error for invalid names
Signed-off-by: Nicolas George <george@nsup.org> --- 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
Signed-off-by: Nicolas George <george@nsup.org> --- libavfilter/vf_format.c | 45 +++++++++++------------------------------ 1 file changed, 12 insertions(+), 33 deletions(-) I thought we were done with confusing copy-paste with an acceptable development tool. This is the --word-diff for the macro: {+#define PARSE_LIST(strfield, fmtfield, tvar, parse) \+} for (char *sep, *cur = [-s->pix_fmts;-]{+s->strfield;+} cur; cur = sep) { {+\+} sep = strchr(cur, '|'); {+\+} if (sep && *sep) {+\+} *sep++ = 0; {+\+} if ((ret = [-parse_pixel_format(&pix_fmt,-]{+parse(&tvar,+} cur, ctx)) < 0 || {+\+} (ret = [-ff_add_format(&s->formats, pix_fmt))-]{+ff_add_format(&s->fmtfield, tvar))+} < 0) {+\+} return ret; {+\+} } diff --git a/libavfilter/vf_format.c b/libavfilter/vf_format.c index 6ab7d9cbcc..fd3cc4a647 100644 --- a/libavfilter/vf_format.c +++ b/libavfilter/vf_format.c @@ -118,41 +118,20 @@ static av_cold int init(AVFilterContext *ctx) enum AVAlphaMode alm; int ret; - for (char *sep, *cur = s->pix_fmts; cur; cur = sep) { - sep = strchr(cur, '|'); - if (sep && *sep) - *sep++ = 0; - if ((ret = parse_pixel_format(&pix_fmt, cur, ctx)) < 0 || - (ret = ff_add_format(&s->formats, pix_fmt)) < 0) - return ret; + #define PARSE_LIST(strfield, fmtfield, tvar, parse) \ + for (char *sep, *cur = s->strfield; cur; cur = sep) { \ + sep = strchr(cur, '|'); \ + if (sep && *sep) \ + *sep++ = 0; \ + if ((ret = parse(&tvar, cur, ctx)) < 0 || \ + (ret = ff_add_format(&s->fmtfield, tvar)) < 0) \ + return ret; \ } - for (char *sep, *cur = s->csps; cur; cur = sep) { - sep = strchr(cur, '|'); - if (sep && *sep) - *sep++ = 0; - if ((ret = parse_color_space(&csp, cur, ctx)) < 0 || - (ret = ff_add_format(&s->color_spaces, csp)) < 0) - return ret; - } - - for (char *sep, *cur = s->ranges; cur; cur = sep) { - sep = strchr(cur, '|'); - if (sep && *sep) - *sep++ = 0; - if ((ret = parse_color_range(&crg, cur, ctx)) < 0 || - (ret = ff_add_format(&s->color_ranges, crg)) < 0) - return ret; - } - - for (char *sep, *cur = s->alphamodes; cur; cur = sep) { - sep = strchr(cur, '|'); - if (sep && *sep) - *sep++ = 0; - if ((ret = parse_alpha_mode(&alm, cur, ctx)) < 0 || - (ret = ff_add_format(&s->alpha_modes, alm)) < 0) - return ret; - } + PARSE_LIST(pix_fmts, formats, pix_fmt, parse_pixel_format) + PARSE_LIST(csps, color_spaces, csp, parse_color_space) + PARSE_LIST(ranges, color_ranges, crg, parse_color_range) + PARSE_LIST(alphamodes, alpha_modes, alm, parse_alpha_mode) if (!strcmp(ctx->filter->name, "noformat")) { if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 || -- 2.53.0
Nicolas George via ffmpeg-devel (HE12026-05-02):
Signed-off-by: Nicolas George <george@nsup.org> --- libavfilter/vf_format.c | 45 +++++++++++------------------------------ 1 file changed, 12 insertions(+), 33 deletions(-)
Pushed both. Regards, -- Nicolas George
participants (1)
-
Nicolas George