Reduce code duplication.
Unified · Split
| @@ -236,6 +236,21 @@ void avfilter_formats_changeref(AVFilterFormats **oldref, · 235 unmodified lines | ||
| 236 | 236 | |
| 237 | 237 | /* internal functions for parsing audio format arguments */ |
| 238 | 238 | |
| 239 | +int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx) | |
| 240 | +{ | |
| 241 | + char *tail; | |
| 242 | + int pix_fmt = av_get_pix_fmt(arg); | |
| 243 | + if (pix_fmt == PIX_FMT_NONE) { | |
| 244 | + pix_fmt = strtol(arg, &tail, 0); | |
| 245 | + if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) { | |
| 246 | + av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg); | |
| 247 | + return AVERROR(EINVAL); | |
| 248 | + } | |
| 249 | + } | |
| 250 | + *ret = pix_fmt; | |
| 251 | + return 0; | |
| 252 | +} | |
| 253 | + | |
| 239 | 254 | int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx) |
| 240 | 255 | { |
| 241 | 256 | char *tail; |
| @@ -63,6 +63,16 @@ int ff_fmt_is_in(int fmt, const int *fmts); · 62 unmodified lines | ||
| 63 | 63 | |
| 64 | 64 | /* Functions to parse audio format arguments */ |
| 65 | 65 | |
| 66 | +/** | |
| 67 | + * Parse a pixel format. | |
| 68 | + * | |
| 69 | + * @param ret pixel format pointer to where the value should be written | |
| 70 | + * @param arg string to parse | |
| 71 | + * @param log_ctx log context | |
| 72 | + * @return 0 in case of success, a negative AVERROR code on error | |
| 73 | + */ | |
| 74 | +int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx); | |
| 75 | + | |
| 66 | 76 | /** |
| 67 | 77 | * Parse a sample rate. |
| 68 | 78 | * |
| @@ -25,6 +25,7 @@ · 24 unmodified lines | ||
| 25 | 25 | |
| 26 | 26 | #include "libavutil/pixdesc.h" |
| 27 | 27 | #include "avfilter.h" |
| 28 | +#include "internal.h" | |
| 28 | 29 | |
| 29 | 30 | typedef struct { |
| 30 | 31 | /** |
| @@ -41,7 +42,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 10 unmodified lines | ||
| 41 | 42 | FormatContext *format = ctx->priv; |
| 42 | 43 | const char *cur, *sep; |
| 43 | 44 | char pix_fmt_name[PIX_FMT_NAME_MAXSIZE]; |
| 44 | − int pix_fmt_name_len; | |
| 45 | + int pix_fmt_name_len, ret; | |
| 45 | 46 | enum PixelFormat pix_fmt; |
| 46 | 47 | |
| 47 | 48 | /* parse the list of formats */ |
| @@ -57,12 +58,9 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 9 unmodified lines | ||
| 57 | 58 | |
| 58 | 59 | memcpy(pix_fmt_name, cur, pix_fmt_name_len); |
| 59 | 60 | pix_fmt_name[pix_fmt_name_len] = 0; |
| 60 | − pix_fmt = av_get_pix_fmt(pix_fmt_name); | |
| 61 | 61 | |
| 62 | − if (pix_fmt == PIX_FMT_NONE) { | |
| 63 | − av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name); | |
| 64 | − return -1; | |
| 65 | − } | |
| 62 | + if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0) | |
| 63 | + return ret; | |
| 66 | 64 | |
| 67 | 65 | format->listed_pix_fmt_flags[pix_fmt] = 1; |
| 68 | 66 | } |
| @@ -24,6 +24,7 @@ · 23 unmodified lines | ||
| 24 | 24 | */ |
| 25 | 25 | |
| 26 | 26 | #include "avfilter.h" |
| 27 | +#include "internal.h" | |
| 27 | 28 | #include "avcodec.h" |
| 28 | 29 | #include "vsrc_buffer.h" |
| 29 | 30 | #include "libavutil/imgutils.h" |
| @@ -134,7 +135,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 104 unmodified lines | ||
| 134 | 135 | { |
| 135 | 136 | BufferSourceContext *c = ctx->priv; |
| 136 | 137 | char pix_fmt_str[128]; |
| 137 | − int n = 0; | |
| 138 | + int ret, n = 0; | |
| 138 | 139 | *c->sws_param = 0; |
| 139 | 140 | |
| 140 | 141 | if (!args || |
| @@ -145,14 +146,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 4 unmodified lines | ||
| 145 | 146 | return AVERROR(EINVAL); |
| 146 | 147 | } |
| 147 | 148 | |
| 148 | − if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) { | |
| 149 | − char *tail; | |
| 150 | − c->pix_fmt = strtol(pix_fmt_str, &tail, 10); | |
| 151 | − if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) { | |
| 152 | − av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str); | |
| 153 | − return AVERROR(EINVAL); | |
| 154 | − } | |
| 155 | − } | |
| 149 | + if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0) | |
| 150 | + return ret; | |
| 156 | 151 | |
| 157 | 152 | av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n", |
| 158 | 153 | c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name, |