]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/eval.c
libvo-amrwbenc: Add braces to shut up gcc warning.
[ffmpeg] / libavutil / eval.c
index 2799160094f30b9993501c09db66d897c65c9144..6d9b8e42086e7b99dad74372cde864de6edafc11 100644 (file)
@@ -2,20 +2,20 @@
  * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
  * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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,
+ * Libav 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
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -26,8 +26,9 @@
  * see http://joe.hotchkiss.com/programming/eval/eval.html
  */
 
-#include "libavutil/avutil.h"
+#include "avutil.h"
 #include "eval.h"
+#include "log.h"
 
 typedef struct Parser {
     const AVClass *class;
@@ -103,22 +104,25 @@ double av_strtod(const char *numstr, char **tail)
     return d;
 }
 
+#define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
+
 static int strmatch(const char *s, const char *prefix)
 {
     int i;
     for (i=0; prefix[i]; i++) {
         if (prefix[i] != s[i]) return 0;
     }
-    return 1;
+    /* return 1 only if the s identifier is terminated */
+    return !IS_IDENTIFIER_CHAR(s[i]);
 }
 
 struct AVExpr {
     enum {
         e_value, e_const, e_func0, e_func1, e_func2,
-        e_squish, e_gauss, e_ld,
+        e_squish, e_gauss, e_ld, e_isnan,
         e_mod, e_max, e_min, e_eq, e_gt, e_gte,
         e_pow, e_mul, e_div, e_add,
-        e_last, e_st, e_while,
+        e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
     } type;
     double value; // is sign in other types
     union {
@@ -141,6 +145,10 @@ static double eval_expr(Parser *p, AVExpr *e)
         case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
         case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
         case e_ld:     return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
+        case e_isnan:  return e->value * !!isnan(eval_expr(p, e->param[0]));
+        case e_floor:  return e->value * floor(eval_expr(p, e->param[0]));
+        case e_ceil :  return e->value * ceil (eval_expr(p, e->param[0]));
+        case e_trunc:  return e->value * trunc(eval_expr(p, e->param[0]));
         case e_while: {
             double d = NAN;
             while (eval_expr(p, e->param[0]))
@@ -171,11 +179,11 @@ static double eval_expr(Parser *p, AVExpr *e)
 
 static int parse_expr(AVExpr **e, Parser *p);
 
-void av_free_expr(AVExpr *e)
+void av_expr_free(AVExpr *e)
 {
     if (!e) return;
-    av_free_expr(e->param[0]);
-    av_free_expr(e->param[1]);
+    av_expr_free(e->param[0]);
+    av_expr_free(e->param[1]);
     av_freep(&e);
 }
 
@@ -213,7 +221,7 @@ static int parse_primary(AVExpr **e, Parser *p)
     if (p->s==NULL) {
         av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
         p->s= next;
-        av_free_expr(d);
+        av_expr_free(d);
         return AVERROR(EINVAL);
     }
     p->s++; // "("
@@ -223,7 +231,7 @@ static int parse_primary(AVExpr **e, Parser *p)
             return ret;
         if (p->s[0] != ')') {
             av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
-            av_free_expr(d);
+            av_expr_free(d);
             return AVERROR(EINVAL);
         }
         p->s++; // ")"
@@ -231,7 +239,7 @@ static int parse_primary(AVExpr **e, Parser *p)
         return 0;
     }
     if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
-        av_free_expr(d);
+        av_expr_free(d);
         return ret;
     }
     if (p->s[0]== ',') {
@@ -240,7 +248,7 @@ static int parse_primary(AVExpr **e, Parser *p)
     }
     if (p->s[0] != ')') {
         av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
-        av_free_expr(d);
+        av_expr_free(d);
         return AVERROR(EINVAL);
     }
     p->s++; // ")"
@@ -269,8 +277,12 @@ static int parse_primary(AVExpr **e, Parser *p)
     else if (strmatch(next, "lte"   )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
     else if (strmatch(next, "lt"    )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
     else if (strmatch(next, "ld"    )) d->type = e_ld;
+    else if (strmatch(next, "isnan" )) d->type = e_isnan;
     else if (strmatch(next, "st"    )) d->type = e_st;
     else if (strmatch(next, "while" )) d->type = e_while;
+    else if (strmatch(next, "floor" )) d->type = e_floor;
+    else if (strmatch(next, "ceil"  )) d->type = e_ceil;
+    else if (strmatch(next, "trunc" )) d->type = e_trunc;
     else {
         for (i=0; p->func1_names && p->func1_names[i]; i++) {
             if (strmatch(next, p->func1_names[i])) {
@@ -291,7 +303,7 @@ static int parse_primary(AVExpr **e, Parser *p)
         }
 
         av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
-        av_free_expr(d);
+        av_expr_free(d);
         return AVERROR(EINVAL);
     }
 
@@ -328,13 +340,13 @@ static int parse_factor(AVExpr **e, Parser *p)
         e1 = e0;
         p->s++;
         if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
-            av_free_expr(e1);
+            av_expr_free(e1);
             return ret;
         }
         e0 = new_eval_expr(e_pow, 1, e1, e2);
         if (!e0) {
-            av_free_expr(e1);
-            av_free_expr(e2);
+            av_expr_free(e1);
+            av_expr_free(e2);
             return AVERROR(ENOMEM);
         }
         if (e0->param[1]) e0->param[1]->value *= (sign2|1);
@@ -355,13 +367,13 @@ static int parse_term(AVExpr **e, Parser *p)
         int c= *p->s++;
         e1 = e0;
         if ((ret = parse_factor(&e2, p)) < 0) {
-            av_free_expr(e1);
+            av_expr_free(e1);
             return ret;
         }
         e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
         if (!e0) {
-            av_free_expr(e1);
-            av_free_expr(e2);
+            av_expr_free(e1);
+            av_expr_free(e2);
             return AVERROR(ENOMEM);
         }
     }
@@ -378,13 +390,13 @@ static int parse_subexpr(AVExpr **e, Parser *p)
     while (*p->s == '+' || *p->s == '-') {
         e1 = e0;
         if ((ret = parse_term(&e2, p)) < 0) {
-            av_free_expr(e1);
+            av_expr_free(e1);
             return ret;
         }
         e0 = new_eval_expr(e_add, 1, e1, e2);
         if (!e0) {
-            av_free_expr(e1);
-            av_free_expr(e2);
+            av_expr_free(e1);
+            av_expr_free(e2);
             return AVERROR(ENOMEM);
         }
     };
@@ -407,13 +419,13 @@ static int parse_expr(AVExpr **e, Parser *p)
         p->s++;
         e1 = e0;
         if ((ret = parse_subexpr(&e2, p)) < 0) {
-            av_free_expr(e1);
+            av_expr_free(e1);
             return ret;
         }
         e0 = new_eval_expr(e_last, 1, e1, e2);
         if (!e0) {
-            av_free_expr(e1);
-            av_free_expr(e2);
+            av_expr_free(e1);
+            av_expr_free(e2);
             return AVERROR(ENOMEM);
         }
     };
@@ -433,18 +445,23 @@ static int verify_expr(AVExpr *e)
         case e_func1:
         case e_squish:
         case e_ld:
-        case e_gauss: return verify_expr(e->param[0]);
+        case e_gauss:
+        case e_isnan:
+        case e_floor:
+        case e_ceil:
+        case e_trunc:
+            return verify_expr(e->param[0]);
         default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
     }
 }
 
-int av_parse_expr(AVExpr **expr, const char *s,
+int av_expr_parse(AVExpr **expr, const char *s,
                   const char * const *const_names,
                   const char * const *func1_names, double (* const *funcs1)(void *, double),
                   const char * const *func2_names, double (* const *funcs2)(void *, double, double),
                   int log_offset, void *log_ctx)
 {
-    Parser p;
+    Parser p = { 0 };
     AVExpr *e = NULL;
     char *w = av_malloc(strlen(s) + 1);
     char *wp = w;
@@ -472,12 +489,13 @@ int av_parse_expr(AVExpr **expr, const char *s,
     if ((ret = parse_expr(&e, &p)) < 0)
         goto end;
     if (*p.s) {
+        av_expr_free(e);
         av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
         ret = AVERROR(EINVAL);
         goto end;
     }
     if (!verify_expr(e)) {
-        av_free_expr(e);
+        av_expr_free(e);
         ret = AVERROR(EINVAL);
         goto end;
     }
@@ -487,35 +505,37 @@ end:
     return ret;
 }
 
-double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
+double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
 {
-    Parser p;
+    Parser p = { 0 };
 
     p.const_values = const_values;
     p.opaque     = opaque;
     return eval_expr(&p, e);
 }
 
-int av_parse_and_eval_expr(double *d, const char *s,
+int av_expr_parse_and_eval(double *d, const char *s,
                            const char * const *const_names, const double *const_values,
                            const char * const *func1_names, double (* const *funcs1)(void *, double),
                            const char * const *func2_names, double (* const *funcs2)(void *, double, double),
                            void *opaque, int log_offset, void *log_ctx)
 {
     AVExpr *e = NULL;
-    int ret = av_parse_expr(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
+    int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
 
     if (ret < 0) {
         *d = NAN;
         return ret;
     }
-    *d = av_eval_expr(e, const_values, opaque);
-    av_free_expr(e);
+    *d = av_expr_eval(e, const_values, opaque);
+    av_expr_free(e);
     return isnan(*d) ? AVERROR(EINVAL) : 0;
 }
 
 #ifdef TEST
 #undef printf
+#include <string.h>
+
 static double const_values[] = {
     M_PI,
     M_E,
@@ -528,7 +548,7 @@ static const char *const_names[] = {
     0
 };
 
-int main(void)
+int main(int argc, char **argv)
 {
     int i;
     double d;
@@ -539,7 +559,7 @@ int main(void)
         "-PI",
         "+PI",
         "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
-        "80G/80Gi"
+        "80G/80Gi",
         "1k",
         "1Gi",
         "1gi",
@@ -571,33 +591,45 @@ int main(void)
         "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
         "while(0, 10)",
         "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
+        "isnan(1)",
+        "isnan(NAN)",
+        "floor(NAN)",
+        "floor(123.123)",
+        "floor(-123.123)",
+        "trunc(123.123)",
+        "trunc(-123.123)",
+        "ceil(123.123)",
+        "ceil(-123.123)",
         NULL
     };
 
     for (expr = exprs; *expr; expr++) {
         printf("Evaluating '%s'\n", *expr);
-        av_parse_and_eval_expr(&d, *expr,
+        av_expr_parse_and_eval(&d, *expr,
                                const_names, const_values,
                                NULL, NULL, NULL, NULL, NULL, 0, NULL);
         printf("'%s' -> %f\n\n", *expr, d);
     }
 
-    av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
+    av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
                            const_names, const_values,
                            NULL, NULL, NULL, NULL, NULL, 0, NULL);
     printf("%f == 12.7\n", d);
-    av_parse_and_eval_expr(&d, "80G/80Gi",
+    av_expr_parse_and_eval(&d, "80G/80Gi",
                            const_names, const_values,
                            NULL, NULL, NULL, NULL, NULL, 0, NULL);
     printf("%f == 0.931322575\n", d);
 
-    for (i=0; i<1050; i++) {
-        START_TIMER
-            av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
+    if (argc > 1 && !strcmp(argv[1], "-t")) {
+        for (i = 0; i < 1050; i++) {
+            START_TIMER;
+            av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
                                    const_names, const_values,
                                    NULL, NULL, NULL, NULL, NULL, 0, NULL);
-        STOP_TIMER("av_parse_and_eval_expr")
+            STOP_TIMER("av_expr_parse_and_eval");
+        }
     }
+
     return 0;
 }
 #endif