[FFmpeg-devel] [PATCH 1/5] lavfi: add internal functions for parsing format arguments

Mina Nagy Zaki mnzaki at gmail.com
Mon Aug 8 10:11:45 CEST 2011


---
 libavfilter/formats.c  |   55 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/internal.h |   38 +++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/libavfilter/formats.c b/libavfilter/formats.c
index 214718b..53d24c0 100644
--- a/libavfilter/formats.c
+++ b/libavfilter/formats.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/eval.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/audioconvert.h"
 #include "avfilter.h"
@@ -233,3 +234,57 @@ void avfilter_formats_changeref(AVFilterFormats **oldref,
     }
 }
 
+/* internal functions for parsing audio format arguments */
+
+int ff_parse_sample_format(char *arg, void *log_ctx)
+{
+    char *tail;
+    int sample_fmt = av_get_sample_fmt(arg);
+    if (sample_fmt == AV_SAMPLE_FMT_NONE) {
+        sample_fmt = strtol(arg, &tail, 0);
+        if (*tail || (unsigned)sample_fmt >= AV_SAMPLE_FMT_NB) {
+            av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
+            return -1;
+        }
+    }
+    return sample_fmt;
+}
+
+int ff_parse_sample_rate(char *arg, void *log_ctx)
+{
+    char *tail;
+    double sample_rate = av_strtod(arg, &tail);
+    if (*tail || sample_rate < 1 || (int)sample_rate != sample_rate) {
+        av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
+        return -1;
+    }
+    return sample_rate;
+}
+
+int64_t ff_parse_channel_layout(char *arg, void *log_ctx)
+{
+    char *tail;
+    int64_t chlayout = av_get_channel_layout(arg);
+    if (chlayout <= 0) {
+        chlayout = strtol(arg, &tail, 0);
+        if (*tail || chlayout <= 0) {
+            av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
+            return -1;
+        }
+    }
+    return chlayout;
+}
+
+int ff_parse_packing_format(char *arg, void *log_ctx)
+{
+    char *tail;
+    int planar = strtol(arg, &tail, 10);
+    if (*tail) {
+        planar = (strcmp(arg, "packed") != 0);
+    } else if (planar != 0 && planar != 1) {
+        av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
+        return -1;
+    }
+    return planar;
+}
+
diff --git a/libavfilter/internal.h b/libavfilter/internal.h
index 7537565..b0a252c 100644
--- a/libavfilter/internal.h
+++ b/libavfilter/internal.h
@@ -61,4 +61,42 @@ void ff_avfilter_default_free_buffer(AVFilterBuffer *buf);
 /** Tell is a format is contained in the provided list terminated by -1. */
 int ff_fmt_is_in(int fmt, const int *fmts);
 
+/* Functions to parse audio format arguments */
+
+/**
+ * Parse a sample rate.
+ *
+ * @param arg string to parse
+ * @param log_ctx log ctx
+ * @return sample rate or -1 on error
+ */
+int ff_parse_sample_rate(char *arg, void *log_ctx);
+
+/**
+ * Parse a sample format name or a corresponding integer representation.
+ *
+ * @param arg string to parse
+ * @param log_ctx log ctx
+ * @return a sample format, or -1 on error
+ */
+int ff_parse_sample_format(char *arg, void *log_ctx);
+
+/**
+ * Parse a channel layout or a corresponding integer representation.
+ *
+ * @param arg string to parse
+ * @param log_ctx log ctx
+ * @return a channel layout, or -1 on error
+ */
+int64_t ff_parse_channel_layout(char *arg, void *log_ctx);
+
+/**
+ * Parse a packing format or a corresponding integer representation.
+ *
+ * @param arg string to parse
+ * @param log_ctx log ctx
+ * @return a packing format, or -1 on error
+ */
+int ff_parse_packing_format(char *arg, void *log_ctx);
+
 #endif /* AVFILTER_INTERNAL_H */
-- 
1.7.4.4



More information about the ffmpeg-devel mailing list