Browse Source

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

Reduce code duplication.
Stefano Sabatini 13 years ago
parent
commit
e26782a9b4
4 changed files with 33 additions and 15 deletions
  1. 15 0
      libavfilter/formats.c
  2. 10 0
      libavfilter/internal.h
  3. 4 6
      libavfilter/vf_format.c
  4. 4 9
      libavfilter/vsrc_buffer.c

+ 15 - 0
libavfilter/formats.c

@@ -236,6 +236,21 @@ void avfilter_formats_changeref(AVFilterFormats **oldref,
 
 /* internal functions for parsing audio format arguments */
 
+int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
+{
+    char *tail;
+    int pix_fmt = av_get_pix_fmt(arg);
+    if (pix_fmt == PIX_FMT_NONE) {
+        pix_fmt = strtol(arg, &tail, 0);
+        if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
+            av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
+            return AVERROR(EINVAL);
+        }
+    }
+    *ret = pix_fmt;
+    return 0;
+}
+
 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
 {
     char *tail;

+ 10 - 0
libavfilter/internal.h

@@ -63,6 +63,16 @@ int ff_fmt_is_in(int fmt, const int *fmts);
 
 /* Functions to parse audio format arguments */
 
+/**
+ * Parse a pixel format.
+ *
+ * @param ret pixel format pointer to where the value should be written
+ * @param arg string to parse
+ * @param log_ctx log context
+ * @return 0 in case of success, a negative AVERROR code on error
+ */
+int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx);
+
 /**
  * Parse a sample rate.
  *

+ 4 - 6
libavfilter/vf_format.c

@@ -25,6 +25,7 @@
 
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
+#include "internal.h"
 
 typedef struct {
     /**
@@ -41,7 +42,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
     FormatContext *format = ctx->priv;
     const char *cur, *sep;
     char             pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
-    int              pix_fmt_name_len;
+    int              pix_fmt_name_len, ret;
     enum PixelFormat pix_fmt;
 
     /* parse the list of formats */
@@ -57,12 +58,9 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 
         memcpy(pix_fmt_name, cur, pix_fmt_name_len);
         pix_fmt_name[pix_fmt_name_len] = 0;
-        pix_fmt = av_get_pix_fmt(pix_fmt_name);
 
-        if (pix_fmt == PIX_FMT_NONE) {
-            av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
-            return -1;
-        }
+        if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
+            return ret;
 
         format->listed_pix_fmt_flags[pix_fmt] = 1;
     }

+ 4 - 9
libavfilter/vsrc_buffer.c

@@ -24,6 +24,7 @@
  */
 
 #include "avfilter.h"
+#include "internal.h"
 #include "avcodec.h"
 #include "vsrc_buffer.h"
 #include "libavutil/imgutils.h"
@@ -134,7 +135,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     BufferSourceContext *c = ctx->priv;
     char pix_fmt_str[128];
-    int n = 0;
+    int ret, n = 0;
     *c->sws_param = 0;
 
     if (!args ||
@@ -145,14 +146,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
         return AVERROR(EINVAL);
     }
 
-    if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
-        char *tail;
-        c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
-        if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
-            av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
-            return AVERROR(EINVAL);
-        }
-    }
+    if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
+        return ret;
 
     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,