[FFmpeg-devel] [PATCH] tools: add ffeval tool

Stefano Sabatini stefasab at gmail.com
Sun Jan 15 23:12:02 CET 2012


The tool is useful for testing the internal arithmetic evaluation engine
(indeed I plan to use it in FATE), and provides an handy calculator when
you can't rely on bc ;-).
---
 libavutil/Makefile |    2 +
 tools/ffeval.c     |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+), 0 deletions(-)
 create mode 100644 tools/ffeval.c

diff --git a/libavutil/Makefile b/libavutil/Makefile
index 7ed590d..b6165a1 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -82,6 +82,8 @@ TESTPROGS = adler32 aes avstring base64 cpu crc des eval file fifo lfg lls \
             md5 opt pca parseutils rational sha tree
 TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo
 
+TOOLS = ffeval
+
 DIRS = arm bfin sh4 x86
 
 ARCH_HEADERS = bswap.h intmath.h intreadwrite.h timer.h
diff --git a/tools/ffeval.c b/tools/ffeval.c
new file mode 100644
index 0000000..8cfa6c5
--- /dev/null
+++ b/tools/ffeval.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2012 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <unistd.h>             /* getopt */
+#include "libavutil/eval.h"
+
+/**
+ * @file
+ * simple arithmetic expression evaluator
+ */
+
+static void usage(void)
+{
+    printf("Simple expression evalutor, please *don't* turn me to a feature-complete interpreter language\n");
+    printf("usage: ffeval [OPTIONS]\n");
+    printf("\n"
+           "Options:\n"
+           "-e                echo the input line on output\n"
+           "-h                print this help\n"
+           "-i INFILE         set INFILE as input file, stdin if omitted\n"
+           "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
+           "-p PROMPT         set PROMPT \n");
+}
+
+int main(int argc, char **argv)
+{
+    size_t buf_size = 256;
+    char *buf = av_malloc(buf_size);
+    const char *outfilename = NULL, *infilename = NULL;
+    FILE *outfile = NULL, *infile = NULL;
+    const char *prompt = "=> ";
+    int count = 0, echo = 0;
+    char c;
+
+    while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
+        switch (c) {
+        case 'e':
+            echo = 1;
+            break;
+        case 'h':
+            usage();
+            return 0;
+        case 'i':
+            infilename = optarg;
+            break;
+        case 'o':
+            outfilename = optarg;
+            break;
+        case 'p':
+            prompt = optarg;
+            break;
+        case '?':
+            return 1;
+        }
+    }
+
+    if (!infilename || !strcmp(infilename, "-"))
+        infilename = "/dev/stdin";
+    infile = fopen(infilename, "r");
+    if (!infile) {
+        fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
+        return 1;
+    }
+
+    if (!outfilename || !strcmp(outfilename, "-"))
+        outfilename = "/dev/stdout";
+    outfile = fopen(outfilename, "w");
+    if (!outfile) {
+        fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
+        return 1;
+    }
+
+    while ((c = fgetc(infile)) != EOF) {
+        if (c == '\n') {
+            double d;
+
+            buf[count] = 0;
+            if (buf[0] != '#') {
+                av_expr_parse_and_eval(&d, buf,
+                                       NULL, NULL,
+                                       NULL, NULL, NULL, NULL, NULL, 0, NULL);
+                if (echo)
+                    fprintf(outfile, "%s ", buf);
+                fprintf(outfile, "%s%f\n", prompt, d);
+            }
+            count = 0;
+        } else {
+            if (count >= buf_size-1) {
+                buf = av_realloc_f(buf, buf_size, 2);
+                if (!buf) {
+                    av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
+                    return 1;
+                }
+            }
+            buf[count++] = c;
+        }
+    }
+
+    return 0;
+}
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list