Browse code

lavfi: add ff_parse_pixel_format() internal function, and use it

Reduce code duplication.

Stefano Sabatini authored on 2011/08/22 21:56:23
Showing 4 changed files
... ...
@@ -236,6 +236,21 @@ void avfilter_formats_changeref(AVFilterFormats **oldref,
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;
... ...
@@ -64,6 +64,16 @@ int ff_fmt_is_in(int fmt, const int *fmts);
64 64
 /* Functions to parse audio format arguments */
65 65
 
66 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
+
76
+/**
67 77
  * Parse a sample rate.
68 78
  *
69 79
  * @param ret unsigned integer pointer to where the value should be written
... ...
@@ -25,6 +25,7 @@
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)
41 41
     FormatContext *format = ctx->priv;
42 42
     const char *cur, *sep;
43 43
     char             pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
44
-    int              pix_fmt_name_len;
44
+    int              pix_fmt_name_len, ret;
45 45
     enum PixelFormat pix_fmt;
46 46
 
47 47
     /* parse the list of formats */
... ...
@@ -57,12 +58,9 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
57 57
 
58 58
         memcpy(pix_fmt_name, cur, pix_fmt_name_len);
59 59
         pix_fmt_name[pix_fmt_name_len] = 0;
60
-        pix_fmt = av_get_pix_fmt(pix_fmt_name);
61 60
 
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
-        }
61
+        if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
62
+            return ret;
66 63
 
67 64
         format->listed_pix_fmt_flags[pix_fmt] = 1;
68 65
     }
... ...
@@ -24,6 +24,7 @@
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)
134 134
 {
135 135
     BufferSourceContext *c = ctx->priv;
136 136
     char pix_fmt_str[128];
137
-    int n = 0;
137
+    int ret, n = 0;
138 138
     *c->sws_param = 0;
139 139
 
140 140
     if (!args ||
... ...
@@ -145,14 +146,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
145 145
         return AVERROR(EINVAL);
146 146
     }
147 147
 
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
-    }
148
+    if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
149
+        return ret;
156 150
 
157 151
     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
158 152
            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,