[FFmpeg-devel] [PATCH] lavfi: add ff_parse_pixel_format() internal function, and use it

Stefano Sabatini stefano.sabatini-lala at poste.it
Mon Aug 22 15:47:43 CEST 2011


Reduce code duplication.
---
 libavfilter/formats.c     |   15 +++++++++++++++
 libavfilter/internal.h    |   10 ++++++++++
 libavfilter/vf_format.c   |   10 ++++------
 libavfilter/vsrc_buffer.c |   13 ++++---------
 4 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/libavfilter/formats.c b/libavfilter/formats.c
index c863be6..1959a0e 100644
--- a/libavfilter/formats.c
+++ b/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;
diff --git a/libavfilter/internal.h b/libavfilter/internal.h
index d03b8b8..b9613d5 100644
--- a/libavfilter/internal.h
+++ b/libavfilter/internal.h
@@ -64,6 +64,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.
  *
  * @param ret unsigned integer pointer to where the value should be written
diff --git a/libavfilter/vf_format.c b/libavfilter/vf_format.c
index 9ecb026..ecf6ebe 100644
--- a/libavfilter/vf_format.c
+++ b/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;
     }
diff --git a/libavfilter/vsrc_buffer.c b/libavfilter/vsrc_buffer.c
index 54867f7..715bd9d 100644
--- a/libavfilter/vsrc_buffer.c
+++ b/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,
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list