[FFmpeg-devel] [PATCH 2/2] Make eval API public.

Stefano Sabatini stefano.sabatini-lala
Tue Jun 1 10:26:10 CEST 2010


---
 libavcodec/Makefile      |    1 -
 libavcodec/avcodec.h     |    2 +-
 libavcodec/eval.c        |  547 ----------------------------------------------
 libavcodec/eval.h        |  128 -----------
 libavcodec/opt.c         |    4 +-
 libavcodec/ratecontrol.c |    8 +-
 libavcodec/ratecontrol.h |    2 +-
 libavutil/Makefile       |    2 +
 libavutil/avutil.h       |    2 +-
 libavutil/eval.c         |  547 ++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/eval.h         |  128 +++++++++++
 11 files changed, 686 insertions(+), 685 deletions(-)
 delete mode 100644 libavcodec/eval.c
 delete mode 100644 libavcodec/eval.h
 create mode 100644 libavutil/eval.c
 create mode 100644 libavutil/eval.h

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 764555e..a5f443c 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -11,7 +11,6 @@ OBJS = allcodecs.o                                                      \
        bitstream.o                                                      \
        bitstream_filter.o                                               \
        dsputil.o                                                        \
-       eval.o                                                           \
        faanidct.o                                                       \
        imgconvert.o                                                     \
        jrevdct.o                                                        \
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index a07e774..9f287d5 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -31,7 +31,7 @@
 
 #define LIBAVCODEC_VERSION_MAJOR 52
 #define LIBAVCODEC_VERSION_MINOR 72
-#define LIBAVCODEC_VERSION_MICRO  1
+#define LIBAVCODEC_VERSION_MICRO  2
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
diff --git a/libavcodec/eval.c b/libavcodec/eval.c
deleted file mode 100644
index 2a8fc44..0000000
--- a/libavcodec/eval.c
+++ /dev/null
@@ -1,547 +0,0 @@
-/*
- * Copyright (c) 2002-2006 Michael Niedermayer <michaelni at gmx.at>
- * Copyright (c) 2006 Oded Shimon <ods15 at ods15.dyndns.org>
- *
- * 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
- */
-
-/**
- * @file
- * simple arithmetic expression evaluator.
- *
- * see http://joe.hotchkiss.com/programming/eval/eval.html
- */
-
-#include "libavutil/avutil.h"
-#include "eval.h"
-
-typedef struct Parser {
-    const AVClass *class;
-    int stack_index;
-    char *s;
-    const double *const_values;
-    const char * const *const_names;          // NULL terminated
-    double (* const *funcs1)(void *, double a);           // NULL terminated
-    const char * const *func1_names;          // NULL terminated
-    double (* const *funcs2)(void *, double a, double b); // NULL terminated
-    const char * const *func2_names;          // NULL terminated
-    void *opaque;
-    int log_offset;
-    void *log_ctx;
-#define VARS 10
-    double var[VARS];
-} Parser;
-
-static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
-
-static const int8_t si_prefixes['z' - 'E' + 1] = {
-    ['y'-'E']= -24,
-    ['z'-'E']= -21,
-    ['a'-'E']= -18,
-    ['f'-'E']= -15,
-    ['p'-'E']= -12,
-    ['n'-'E']= - 9,
-    ['u'-'E']= - 6,
-    ['m'-'E']= - 3,
-    ['c'-'E']= - 2,
-    ['d'-'E']= - 1,
-    ['h'-'E']=   2,
-    ['k'-'E']=   3,
-    ['K'-'E']=   3,
-    ['M'-'E']=   6,
-    ['G'-'E']=   9,
-    ['T'-'E']=  12,
-    ['P'-'E']=  15,
-    ['E'-'E']=  18,
-    ['Z'-'E']=  21,
-    ['Y'-'E']=  24,
-};
-
-double av_strtod(const char *numstr, char **tail)
-{
-    double d;
-    char *next;
-    d = strtod(numstr, &next);
-    /* if parsing succeeded, check for and interpret postfixes */
-    if (next!=numstr) {
-        if (*next >= 'E' && *next <= 'z') {
-            int e= si_prefixes[*next - 'E'];
-            if (e) {
-                if (next[1] == 'i') {
-                    d*= pow( 2, e/0.3);
-                    next+=2;
-                } else {
-                    d*= pow(10, e);
-                    next++;
-                }
-            }
-        }
-
-        if (*next=='B') {
-            d*=8;
-            next++;
-        }
-    }
-    /* if requested, fill in tail with the position after the last parsed
-       character */
-    if (tail)
-        *tail = next;
-    return d;
-}
-
-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;
-}
-
-struct AVExpr {
-    enum {
-        e_value, e_const, e_func0, e_func1, e_func2,
-        e_squish, e_gauss, e_ld,
-        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,
-    } type;
-    double value; // is sign in other types
-    union {
-        int const_index;
-        double (*func0)(double);
-        double (*func1)(void *, double);
-        double (*func2)(void *, double, double);
-    } a;
-    struct AVExpr *param[2];
-};
-
-static double eval_expr(Parser *p, AVExpr *e)
-{
-    switch (e->type) {
-        case e_value:  return e->value;
-        case e_const:  return e->value * p->const_values[e->a.const_index];
-        case e_func0:  return e->value * e->a.func0(eval_expr(p, e->param[0]));
-        case e_func1:  return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
-        case e_func2:  return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
-        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_while: {
-            double d = NAN;
-            while (eval_expr(p, e->param[0]))
-                d=eval_expr(p, e->param[1]);
-            return d;
-        }
-        default: {
-            double d = eval_expr(p, e->param[0]);
-            double d2 = eval_expr(p, e->param[1]);
-            switch (e->type) {
-                case e_mod: return e->value * (d - floor(d/d2)*d2);
-                case e_max: return e->value * (d >  d2 ?   d : d2);
-                case e_min: return e->value * (d <  d2 ?   d : d2);
-                case e_eq:  return e->value * (d == d2 ? 1.0 : 0.0);
-                case e_gt:  return e->value * (d >  d2 ? 1.0 : 0.0);
-                case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
-                case e_pow: return e->value * pow(d, d2);
-                case e_mul: return e->value * (d * d2);
-                case e_div: return e->value * (d / d2);
-                case e_add: return e->value * (d + d2);
-                case e_last:return e->value * d2;
-                case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
-            }
-        }
-    }
-    return NAN;
-}
-
-static int parse_expr(AVExpr **e, Parser *p);
-
-void ff_free_expr(AVExpr *e)
-{
-    if (!e) return;
-    ff_free_expr(e->param[0]);
-    ff_free_expr(e->param[1]);
-    av_freep(&e);
-}
-
-static int parse_primary(AVExpr **e, Parser *p)
-{
-    AVExpr *d = av_mallocz(sizeof(AVExpr));
-    char *next= p->s;
-    int ret, i;
-
-    if (!d)
-        return AVERROR(ENOMEM);
-
-    /* number */
-    d->value = av_strtod(p->s, &next);
-    if (next != p->s) {
-        d->type = e_value;
-        p->s= next;
-        *e = d;
-        return 0;
-    }
-    d->value = 1;
-
-    /* named constants */
-    for (i=0; p->const_names && p->const_names[i]; i++) {
-        if (strmatch(p->s, p->const_names[i])) {
-            p->s+= strlen(p->const_names[i]);
-            d->type = e_const;
-            d->a.const_index = i;
-            *e = d;
-            return 0;
-        }
-    }
-
-    p->s= strchr(p->s, '(');
-    if (p->s==NULL) {
-        av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n");
-        p->s= next;
-        ff_free_expr(d);
-        return AVERROR(EINVAL);
-    }
-    p->s++; // "("
-    if (*next == '(') { // special case do-nothing
-        av_freep(&d);
-        if ((ret = parse_expr(&d, p)) < 0)
-            return ret;
-        if (p->s[0] != ')') {
-            av_log(p, AV_LOG_ERROR, "missing )\n");
-            ff_free_expr(d);
-            return AVERROR(EINVAL);
-        }
-        p->s++; // ")"
-        *e = d;
-        return 0;
-    }
-    if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
-        ff_free_expr(d);
-        return ret;
-    }
-    if (p->s[0]== ',') {
-        p->s++; // ","
-        parse_expr(&d->param[1], p);
-    }
-    if (p->s[0] != ')') {
-        av_log(p, AV_LOG_ERROR, "missing )\n");
-        ff_free_expr(d);
-        return AVERROR(EINVAL);
-    }
-    p->s++; // ")"
-
-    d->type = e_func0;
-         if (strmatch(next, "sinh"  )) d->a.func0 = sinh;
-    else if (strmatch(next, "cosh"  )) d->a.func0 = cosh;
-    else if (strmatch(next, "tanh"  )) d->a.func0 = tanh;
-    else if (strmatch(next, "sin"   )) d->a.func0 = sin;
-    else if (strmatch(next, "cos"   )) d->a.func0 = cos;
-    else if (strmatch(next, "tan"   )) d->a.func0 = tan;
-    else if (strmatch(next, "atan"  )) d->a.func0 = atan;
-    else if (strmatch(next, "asin"  )) d->a.func0 = asin;
-    else if (strmatch(next, "acos"  )) d->a.func0 = acos;
-    else if (strmatch(next, "exp"   )) d->a.func0 = exp;
-    else if (strmatch(next, "log"   )) d->a.func0 = log;
-    else if (strmatch(next, "abs"   )) d->a.func0 = fabs;
-    else if (strmatch(next, "squish")) d->type = e_squish;
-    else if (strmatch(next, "gauss" )) d->type = e_gauss;
-    else if (strmatch(next, "mod"   )) d->type = e_mod;
-    else if (strmatch(next, "max"   )) d->type = e_max;
-    else if (strmatch(next, "min"   )) d->type = e_min;
-    else if (strmatch(next, "eq"    )) d->type = e_eq;
-    else if (strmatch(next, "gte"   )) d->type = e_gte;
-    else if (strmatch(next, "gt"    )) d->type = e_gt;
-    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, "st"    )) d->type = e_st;
-    else if (strmatch(next, "while" )) d->type = e_while;
-    else {
-        for (i=0; p->func1_names && p->func1_names[i]; i++) {
-            if (strmatch(next, p->func1_names[i])) {
-                d->a.func1 = p->funcs1[i];
-                d->type = e_func1;
-                *e = d;
-                return 0;
-            }
-        }
-
-        for (i=0; p->func2_names && p->func2_names[i]; i++) {
-            if (strmatch(next, p->func2_names[i])) {
-                d->a.func2 = p->funcs2[i];
-                d->type = e_func2;
-                *e = d;
-                return 0;
-            }
-        }
-
-        av_log(p, AV_LOG_ERROR, "unknown function\n");
-        ff_free_expr(d);
-        return AVERROR(EINVAL);
-    }
-
-    *e = d;
-    return 0;
-}
-
-static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
-{
-    AVExpr *e = av_mallocz(sizeof(AVExpr));
-    if (!e)
-        return NULL;
-    e->type     =type   ;
-    e->value    =value  ;
-    e->param[0] =p0     ;
-    e->param[1] =p1     ;
-    return e;
-}
-
-static int parse_pow(AVExpr **e, Parser *p, int *sign)
-{
-    *sign= (*p->s == '+') - (*p->s == '-');
-    p->s += *sign&1;
-    return parse_primary(e, p);
-}
-
-static int parse_factor(AVExpr **e, Parser *p)
-{
-    int sign, sign2, ret;
-    AVExpr *e0, *e1, *e2;
-    if ((ret = parse_pow(&e0, p, &sign)) < 0)
-        return ret;
-    while(p->s[0]=='^'){
-        e1 = e0;
-        p->s++;
-        if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
-            ff_free_expr(e1);
-            return ret;
-        }
-        e0 = new_eval_expr(e_pow, 1, e1, e2);
-        if (!e0) {
-            ff_free_expr(e1);
-            ff_free_expr(e2);
-            return AVERROR(ENOMEM);
-        }
-        if (e0->param[1]) e0->param[1]->value *= (sign2|1);
-    }
-    if (e0) e0->value *= (sign|1);
-
-    *e = e0;
-    return 0;
-}
-
-static int parse_term(AVExpr **e, Parser *p)
-{
-    int ret;
-    AVExpr *e0, *e1, *e2;
-    if ((ret = parse_factor(&e0, p)) < 0)
-        return ret;
-    while (p->s[0]=='*' || p->s[0]=='/') {
-        int c= *p->s++;
-        e1 = e0;
-        if ((ret = parse_factor(&e2, p)) < 0) {
-            ff_free_expr(e1);
-            return ret;
-        }
-        e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
-        if (!e0) {
-            ff_free_expr(e1);
-            ff_free_expr(e2);
-            return AVERROR(ENOMEM);
-        }
-    }
-    *e = e0;
-    return 0;
-}
-
-static int parse_subexpr(AVExpr **e, Parser *p)
-{
-    int ret;
-    AVExpr *e0, *e1, *e2;
-    if ((ret = parse_term(&e0, p)) < 0)
-        return ret;
-    while (*p->s == '+' || *p->s == '-') {
-        e1 = e0;
-        if ((ret = parse_term(&e2, p)) < 0) {
-            ff_free_expr(e1);
-            return ret;
-        }
-        e0 = new_eval_expr(e_add, 1, e1, e2);
-        if (!e0) {
-            ff_free_expr(e1);
-            ff_free_expr(e2);
-            return AVERROR(ENOMEM);
-        }
-    };
-
-    *e = e0;
-    return 0;
-}
-
-static int parse_expr(AVExpr **e, Parser *p)
-{
-    int ret;
-    AVExpr *e0, *e1, *e2;
-    if (p->stack_index <= 0) //protect against stack overflows
-        return AVERROR(EINVAL);
-    p->stack_index--;
-
-    if ((ret = parse_subexpr(&e0, p)) < 0)
-        return ret;
-    while (*p->s == ';') {
-        e1 = e0;
-        if ((ret = parse_subexpr(&e2, p)) < 0) {
-            ff_free_expr(e1);
-            return ret;
-        }
-        p->s++;
-        e0 = new_eval_expr(e_last, 1, e1, e2);
-        if (!e0) {
-            ff_free_expr(e1);
-            ff_free_expr(e2);
-            return AVERROR(ENOMEM);
-        }
-    };
-
-    p->stack_index++;
-    *e = e0;
-    return 0;
-}
-
-static int verify_expr(AVExpr *e)
-{
-    if (!e) return 0;
-    switch (e->type) {
-        case e_value:
-        case e_const: return 1;
-        case e_func0:
-        case e_func1:
-        case e_squish:
-        case e_ld:
-        case e_gauss: return verify_expr(e->param[0]);
-        default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
-    }
-}
-
-int ff_parse_expr(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;
-    AVExpr *e = NULL;
-    char *w = av_malloc(strlen(s) + 1);
-    char *wp = w;
-    int ret = 0;
-
-    if (!w)
-        return AVERROR(ENOMEM);
-
-    while (*s)
-        if (!isspace(*s++)) *wp++ = s[-1];
-    *wp++ = 0;
-
-    p.class      = &class;
-    p.stack_index=100;
-    p.s= w;
-    p.const_names = const_names;
-    p.funcs1      = funcs1;
-    p.func1_names = func1_names;
-    p.funcs2      = funcs2;
-    p.func2_names = func2_names;
-    p.log_offset = log_offset;
-    p.log_ctx    = log_ctx;
-
-    if ((ret = parse_expr(&e, &p)) < 0)
-        goto end;
-    if (!verify_expr(e)) {
-        ff_free_expr(e);
-        ret = AVERROR(EINVAL);
-        goto end;
-    }
-    *expr = e;
-end:
-    av_free(w);
-    return ret;
-}
-
-double ff_eval_expr(AVExpr *e, const double *const_values, void *opaque)
-{
-    Parser p;
-
-    p.const_values = const_values;
-    p.opaque     = opaque;
-    return eval_expr(&p, e);
-}
-
-int ff_parse_and_eval_expr(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 = ff_parse_expr(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
-
-    if (ret < 0) {
-        *d = NAN;
-        return ret;
-    }
-    *d = ff_eval_expr(e, const_values, opaque);
-    ff_free_expr(e);
-    return isnan(*d) ? AVERROR(EINVAL) : 0;
-}
-
-#ifdef TEST
-#undef printf
-static double const_values[] = {
-    M_PI,
-    M_E,
-    0
-};
-
-static const char *const_names[] = {
-    "PI",
-    "E",
-    0
-};
-
-int main(void)
-{
-    int i;
-    double d;
-    ff_parse_and_eval_expr(&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);
-    ff_parse_and_eval_expr(&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
-            ff_parse_and_eval_expr(&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("ff_parse_and_eval_expr")
-    }
-    return 0;
-}
-#endif
diff --git a/libavcodec/eval.h b/libavcodec/eval.h
deleted file mode 100644
index 1db2b0c..0000000
--- a/libavcodec/eval.h
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright (c) 2002 Michael Niedermayer <michaelni at gmx.at>
- *
- * 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
- */
-
-/**
- * @file
- * simple arithmetic expression evaluator
- */
-
-#ifndef AVCODEC_EVAL_H
-#define AVCODEC_EVAL_H
-
-typedef struct AVExpr AVExpr;
-
-/**
- * Parses and evaluates an expression.
- * Note, this is significantly slower than ff_eval_expr().
- *
- * @param res a pointer to a double where is put the result value of
- *        the expression, or NAN in case of invalid expression
- * @param s expression as a zero terminated string, for example
- *        "1+2^3+5*5+sin(2/3)"
- * @param const_names NULL terminated array of zero terminated strings
- *        of constant identifers, for example {"PI", "E", 0}
- * @param const_values a zero terminated array of values for the
- *        identifers from const_names
- * @param func1_names NULL terminated array of zero terminated strings
- *        of funcs1 identifers
- * @param funcs1 NULL terminated array of function pointers for
- *        functions which take 1 argument
- * @param func2_names NULL terminated array of zero terminated strings
- *        of funcs2 identifers
- * @param funcs2 NULL terminated array of function pointers for
- *        functions which take 2 arguments
- * @param opaque a pointer which will be passed to all functions from
- *        funcs1 and funcs2
- * @param log_ctx parent logging context
- * @return 0 in case of success, a negative value corresponding to an
- *        AVERROR code otherwise
- */
-int ff_parse_and_eval_expr(double *res, 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);
-
-/**
- * Parses an expression.
- *
- * @param expr a pointer where is put an AVExpr containing the parsed
- *        value in case of successfull parsing, or NULL otherwise.
- *        The pointed to AVExpr must be freed with ff_free_expr() by
- *        the user when it is not needed anymore.
- * @param s expression as a zero terminated string, for example
- *        "1+2^3+5*5+sin(2/3)"
- * @param const_names NULL terminated array of zero terminated strings
- *        of constant identifers, for example {"PI", "E", 0}
- * @param func1_names NULL terminated array of zero terminated strings
- *        of funcs1 identifers
- * @param funcs1 NULL terminated array of function pointers for
- *        functions which take 1 argument
- * @param func2_names NULL terminated array of zero terminated strings
- *        of funcs2 identifers
- * @param funcs2 NULL terminated array of function pointers for
- *        functions which take 2 arguments
- * @param log_ctx parent logging context
- * @return 0 in case of success, a negative value corresponding to an
- *        AVERROR code otherwise
- */
-int ff_parse_expr(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);
-
-/**
- * Evaluates a previously parsed expression.
- *
- * @param const_values a zero terminated array of values for the
- *        identifers from ff_parse() const_names
- * @param opaque a pointer which will be passed to all functions from
- *        funcs1 and funcs2
- * @return the value of the expression
- */
-double ff_eval_expr(AVExpr *e, const double *const_values, void *opaque);
-
-/**
- * Frees a parsed expression previously created with ff_parse_expr().
- */
-void ff_free_expr(AVExpr *e);
-
-/**
- * Parses the string in numstr and returns its value as a double. If
- * the string is empty, contains only whitespaces, or does not contain
- * an initial substring that has the expected syntax for a
- * floating-point number, no conversion is performed. In this case,
- * returns a value of zero and the value returned in tail is the value
- * of numstr.
- *
- * @param numstr a string representing a number, may contain one of
- *        the International System number postfixes, for example 'K',
- *        'M', 'G'. If 'i' is appended after the postfix, powers of 2
- *        are used instead of powers of 10. The 'B' postfix multiplies
- *        the value for 8, and can be appended after another postfix
- *        or used alone. This allows using for example 'KB', 'MiB',
- *        'G' and 'B' as postfix.
- * @param tail if non-NULL puts here the pointer to the char next
- *        after the last parsed character
- */
-double av_strtod(const char *numstr, char **tail);
-
-#endif /* AVCODEC_EVAL_H */
diff --git a/libavcodec/opt.c b/libavcodec/opt.c
index 8473d90..128d95d 100644
--- a/libavcodec/opt.c
+++ b/libavcodec/opt.c
@@ -27,7 +27,7 @@
 
 #include "avcodec.h"
 #include "opt.h"
-#include "eval.h"
+#include "libavutil/eval.h"
 
 //FIXME order them and do a bin search
 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags){
@@ -165,7 +165,7 @@ int av_set_string3(void *obj, const char *name, const char *val, int alloc, cons
                 else if(!strcmp(buf, "none"   )) d= 0;
                 else if(!strcmp(buf, "all"    )) d= ~0;
                 else {
-                    int res = ff_parse_and_eval_expr(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
+                    int res = av_parse_and_eval_expr(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
                     if (res < 0) {
                         av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
                         return res;
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 375815a..69ed0c9 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -30,7 +30,7 @@
 #include "dsputil.h"
 #include "ratecontrol.h"
 #include "mpegvideo.h"
-#include "eval.h"
+#include "libavutil/eval.h"
 
 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
 #include <assert.h>
@@ -106,7 +106,7 @@ int ff_rate_control_init(MpegEncContext *s)
     };
     emms_c();
 
-    res = ff_parse_expr(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx);
+    res = av_parse_expr(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx);
     if (res < 0) {
         av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq);
         return res;
@@ -254,7 +254,7 @@ void ff_rate_control_uninit(MpegEncContext *s)
     RateControlContext *rcc= &s->rc_context;
     emms_c();
 
-    ff_free_expr(rcc->rc_eq_eval);
+    av_free_expr(rcc->rc_eq_eval);
     av_freep(&rcc->entry);
 
 #if CONFIG_LIBXVID
@@ -338,7 +338,7 @@ static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_f
         0
     };
 
-    bits= ff_eval_expr(rcc->rc_eq_eval, const_values, rce);
+    bits= av_eval_expr(rcc->rc_eq_eval, const_values, rce);
     if (isnan(bits)) {
         av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", s->avctx->rc_eq);
         return -1;
diff --git a/libavcodec/ratecontrol.h b/libavcodec/ratecontrol.h
index d5fe2bc..32efe01 100644
--- a/libavcodec/ratecontrol.h
+++ b/libavcodec/ratecontrol.h
@@ -30,7 +30,7 @@
 
 #include <stdio.h>
 #include <stdint.h>
-#include "eval.h"
+#include "libavutil/eval.h"
 
 typedef struct Predictor{
     double coeff;
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 6035ba6..5a9acd2 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -9,6 +9,7 @@ HEADERS = adler32.h                                                     \
           base64.h                                                      \
           common.h                                                      \
           crc.h                                                         \
+          eval.h                                                        \
           error.h                                                       \
           fifo.h                                                        \
           intfloat_readwrite.h                                          \
@@ -31,6 +32,7 @@ OBJS = adler32.o                                                        \
        base64.o                                                         \
        crc.o                                                            \
        des.o                                                            \
+       eval.o                                                           \
        error.o                                                          \
        fifo.o                                                           \
        intfloat_readwrite.o                                             \
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 72bd2c5..b23f42f 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -40,7 +40,7 @@
 #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
 
 #define LIBAVUTIL_VERSION_MAJOR 50
-#define LIBAVUTIL_VERSION_MINOR 16
+#define LIBAVUTIL_VERSION_MINOR 17
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
diff --git a/libavutil/eval.c b/libavutil/eval.c
new file mode 100644
index 0000000..59ebd94
--- /dev/null
+++ b/libavutil/eval.c
@@ -0,0 +1,547 @@
+/*
+ * Copyright (c) 2002-2006 Michael Niedermayer <michaelni at gmx.at>
+ * Copyright (c) 2006 Oded Shimon <ods15 at ods15.dyndns.org>
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * simple arithmetic expression evaluator.
+ *
+ * see http://joe.hotchkiss.com/programming/eval/eval.html
+ */
+
+#include "libavutil/avutil.h"
+#include "eval.h"
+
+typedef struct Parser {
+    const AVClass *class;
+    int stack_index;
+    char *s;
+    const double *const_values;
+    const char * const *const_names;          // NULL terminated
+    double (* const *funcs1)(void *, double a);           // NULL terminated
+    const char * const *func1_names;          // NULL terminated
+    double (* const *funcs2)(void *, double a, double b); // NULL terminated
+    const char * const *func2_names;          // NULL terminated
+    void *opaque;
+    int log_offset;
+    void *log_ctx;
+#define VARS 10
+    double var[VARS];
+} Parser;
+
+static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
+
+static const int8_t si_prefixes['z' - 'E' + 1] = {
+    ['y'-'E']= -24,
+    ['z'-'E']= -21,
+    ['a'-'E']= -18,
+    ['f'-'E']= -15,
+    ['p'-'E']= -12,
+    ['n'-'E']= - 9,
+    ['u'-'E']= - 6,
+    ['m'-'E']= - 3,
+    ['c'-'E']= - 2,
+    ['d'-'E']= - 1,
+    ['h'-'E']=   2,
+    ['k'-'E']=   3,
+    ['K'-'E']=   3,
+    ['M'-'E']=   6,
+    ['G'-'E']=   9,
+    ['T'-'E']=  12,
+    ['P'-'E']=  15,
+    ['E'-'E']=  18,
+    ['Z'-'E']=  21,
+    ['Y'-'E']=  24,
+};
+
+double av_strtod(const char *numstr, char **tail)
+{
+    double d;
+    char *next;
+    d = strtod(numstr, &next);
+    /* if parsing succeeded, check for and interpret postfixes */
+    if (next!=numstr) {
+        if (*next >= 'E' && *next <= 'z') {
+            int e= si_prefixes[*next - 'E'];
+            if (e) {
+                if (next[1] == 'i') {
+                    d*= pow( 2, e/0.3);
+                    next+=2;
+                } else {
+                    d*= pow(10, e);
+                    next++;
+                }
+            }
+        }
+
+        if (*next=='B') {
+            d*=8;
+            next++;
+        }
+    }
+    /* if requested, fill in tail with the position after the last parsed
+       character */
+    if (tail)
+        *tail = next;
+    return d;
+}
+
+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;
+}
+
+struct AVExpr {
+    enum {
+        e_value, e_const, e_func0, e_func1, e_func2,
+        e_squish, e_gauss, e_ld,
+        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,
+    } type;
+    double value; // is sign in other types
+    union {
+        int const_index;
+        double (*func0)(double);
+        double (*func1)(void *, double);
+        double (*func2)(void *, double, double);
+    } a;
+    struct AVExpr *param[2];
+};
+
+static double eval_expr(Parser *p, AVExpr *e)
+{
+    switch (e->type) {
+        case e_value:  return e->value;
+        case e_const:  return e->value * p->const_values[e->a.const_index];
+        case e_func0:  return e->value * e->a.func0(eval_expr(p, e->param[0]));
+        case e_func1:  return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
+        case e_func2:  return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
+        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_while: {
+            double d = NAN;
+            while (eval_expr(p, e->param[0]))
+                d=eval_expr(p, e->param[1]);
+            return d;
+        }
+        default: {
+            double d = eval_expr(p, e->param[0]);
+            double d2 = eval_expr(p, e->param[1]);
+            switch (e->type) {
+                case e_mod: return e->value * (d - floor(d/d2)*d2);
+                case e_max: return e->value * (d >  d2 ?   d : d2);
+                case e_min: return e->value * (d <  d2 ?   d : d2);
+                case e_eq:  return e->value * (d == d2 ? 1.0 : 0.0);
+                case e_gt:  return e->value * (d >  d2 ? 1.0 : 0.0);
+                case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
+                case e_pow: return e->value * pow(d, d2);
+                case e_mul: return e->value * (d * d2);
+                case e_div: return e->value * (d / d2);
+                case e_add: return e->value * (d + d2);
+                case e_last:return e->value * d2;
+                case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
+            }
+        }
+    }
+    return NAN;
+}
+
+static int parse_expr(AVExpr **e, Parser *p);
+
+void av_free_expr(AVExpr *e)
+{
+    if (!e) return;
+    av_free_expr(e->param[0]);
+    av_free_expr(e->param[1]);
+    av_freep(&e);
+}
+
+static int parse_primary(AVExpr **e, Parser *p)
+{
+    AVExpr *d = av_mallocz(sizeof(AVExpr));
+    char *next= p->s;
+    int ret, i;
+
+    if (!d)
+        return AVERROR(ENOMEM);
+
+    /* number */
+    d->value = av_strtod(p->s, &next);
+    if (next != p->s) {
+        d->type = e_value;
+        p->s= next;
+        *e = d;
+        return 0;
+    }
+    d->value = 1;
+
+    /* named constants */
+    for (i=0; p->const_names && p->const_names[i]; i++) {
+        if (strmatch(p->s, p->const_names[i])) {
+            p->s+= strlen(p->const_names[i]);
+            d->type = e_const;
+            d->a.const_index = i;
+            *e = d;
+            return 0;
+        }
+    }
+
+    p->s= strchr(p->s, '(');
+    if (p->s==NULL) {
+        av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n");
+        p->s= next;
+        av_free_expr(d);
+        return AVERROR(EINVAL);
+    }
+    p->s++; // "("
+    if (*next == '(') { // special case do-nothing
+        av_freep(&d);
+        if ((ret = parse_expr(&d, p)) < 0)
+            return ret;
+        if (p->s[0] != ')') {
+            av_log(p, AV_LOG_ERROR, "missing )\n");
+            av_free_expr(d);
+            return AVERROR(EINVAL);
+        }
+        p->s++; // ")"
+        *e = d;
+        return 0;
+    }
+    if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
+        av_free_expr(d);
+        return ret;
+    }
+    if (p->s[0]== ',') {
+        p->s++; // ","
+        parse_expr(&d->param[1], p);
+    }
+    if (p->s[0] != ')') {
+        av_log(p, AV_LOG_ERROR, "missing )\n");
+        av_free_expr(d);
+        return AVERROR(EINVAL);
+    }
+    p->s++; // ")"
+
+    d->type = e_func0;
+         if (strmatch(next, "sinh"  )) d->a.func0 = sinh;
+    else if (strmatch(next, "cosh"  )) d->a.func0 = cosh;
+    else if (strmatch(next, "tanh"  )) d->a.func0 = tanh;
+    else if (strmatch(next, "sin"   )) d->a.func0 = sin;
+    else if (strmatch(next, "cos"   )) d->a.func0 = cos;
+    else if (strmatch(next, "tan"   )) d->a.func0 = tan;
+    else if (strmatch(next, "atan"  )) d->a.func0 = atan;
+    else if (strmatch(next, "asin"  )) d->a.func0 = asin;
+    else if (strmatch(next, "acos"  )) d->a.func0 = acos;
+    else if (strmatch(next, "exp"   )) d->a.func0 = exp;
+    else if (strmatch(next, "log"   )) d->a.func0 = log;
+    else if (strmatch(next, "abs"   )) d->a.func0 = fabs;
+    else if (strmatch(next, "squish")) d->type = e_squish;
+    else if (strmatch(next, "gauss" )) d->type = e_gauss;
+    else if (strmatch(next, "mod"   )) d->type = e_mod;
+    else if (strmatch(next, "max"   )) d->type = e_max;
+    else if (strmatch(next, "min"   )) d->type = e_min;
+    else if (strmatch(next, "eq"    )) d->type = e_eq;
+    else if (strmatch(next, "gte"   )) d->type = e_gte;
+    else if (strmatch(next, "gt"    )) d->type = e_gt;
+    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, "st"    )) d->type = e_st;
+    else if (strmatch(next, "while" )) d->type = e_while;
+    else {
+        for (i=0; p->func1_names && p->func1_names[i]; i++) {
+            if (strmatch(next, p->func1_names[i])) {
+                d->a.func1 = p->funcs1[i];
+                d->type = e_func1;
+                *e = d;
+                return 0;
+            }
+        }
+
+        for (i=0; p->func2_names && p->func2_names[i]; i++) {
+            if (strmatch(next, p->func2_names[i])) {
+                d->a.func2 = p->funcs2[i];
+                d->type = e_func2;
+                *e = d;
+                return 0;
+            }
+        }
+
+        av_log(p, AV_LOG_ERROR, "unknown function\n");
+        av_free_expr(d);
+        return AVERROR(EINVAL);
+    }
+
+    *e = d;
+    return 0;
+}
+
+static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
+{
+    AVExpr *e = av_mallocz(sizeof(AVExpr));
+    if (!e)
+        return NULL;
+    e->type     =type   ;
+    e->value    =value  ;
+    e->param[0] =p0     ;
+    e->param[1] =p1     ;
+    return e;
+}
+
+static int parse_pow(AVExpr **e, Parser *p, int *sign)
+{
+    *sign= (*p->s == '+') - (*p->s == '-');
+    p->s += *sign&1;
+    return parse_primary(e, p);
+}
+
+static int parse_factor(AVExpr **e, Parser *p)
+{
+    int sign, sign2, ret;
+    AVExpr *e0, *e1, *e2;
+    if ((ret = parse_pow(&e0, p, &sign)) < 0)
+        return ret;
+    while(p->s[0]=='^'){
+        e1 = e0;
+        p->s++;
+        if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
+            av_free_expr(e1);
+            return ret;
+        }
+        e0 = new_eval_expr(e_pow, 1, e1, e2);
+        if (!e0) {
+            av_free_expr(e1);
+            av_free_expr(e2);
+            return AVERROR(ENOMEM);
+        }
+        if (e0->param[1]) e0->param[1]->value *= (sign2|1);
+    }
+    if (e0) e0->value *= (sign|1);
+
+    *e = e0;
+    return 0;
+}
+
+static int parse_term(AVExpr **e, Parser *p)
+{
+    int ret;
+    AVExpr *e0, *e1, *e2;
+    if ((ret = parse_factor(&e0, p)) < 0)
+        return ret;
+    while (p->s[0]=='*' || p->s[0]=='/') {
+        int c= *p->s++;
+        e1 = e0;
+        if ((ret = parse_factor(&e2, p)) < 0) {
+            av_free_expr(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);
+            return AVERROR(ENOMEM);
+        }
+    }
+    *e = e0;
+    return 0;
+}
+
+static int parse_subexpr(AVExpr **e, Parser *p)
+{
+    int ret;
+    AVExpr *e0, *e1, *e2;
+    if ((ret = parse_term(&e0, p)) < 0)
+        return ret;
+    while (*p->s == '+' || *p->s == '-') {
+        e1 = e0;
+        if ((ret = parse_term(&e2, p)) < 0) {
+            av_free_expr(e1);
+            return ret;
+        }
+        e0 = new_eval_expr(e_add, 1, e1, e2);
+        if (!e0) {
+            av_free_expr(e1);
+            av_free_expr(e2);
+            return AVERROR(ENOMEM);
+        }
+    };
+
+    *e = e0;
+    return 0;
+}
+
+static int parse_expr(AVExpr **e, Parser *p)
+{
+    int ret;
+    AVExpr *e0, *e1, *e2;
+    if (p->stack_index <= 0) //protect against stack overflows
+        return AVERROR(EINVAL);
+    p->stack_index--;
+
+    if ((ret = parse_subexpr(&e0, p)) < 0)
+        return ret;
+    while (*p->s == ';') {
+        e1 = e0;
+        if ((ret = parse_subexpr(&e2, p)) < 0) {
+            av_free_expr(e1);
+            return ret;
+        }
+        p->s++;
+        e0 = new_eval_expr(e_last, 1, e1, e2);
+        if (!e0) {
+            av_free_expr(e1);
+            av_free_expr(e2);
+            return AVERROR(ENOMEM);
+        }
+    };
+
+    p->stack_index++;
+    *e = e0;
+    return 0;
+}
+
+static int verify_expr(AVExpr *e)
+{
+    if (!e) return 0;
+    switch (e->type) {
+        case e_value:
+        case e_const: return 1;
+        case e_func0:
+        case e_func1:
+        case e_squish:
+        case e_ld:
+        case e_gauss: 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,
+                  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;
+    AVExpr *e = NULL;
+    char *w = av_malloc(strlen(s) + 1);
+    char *wp = w;
+    int ret = 0;
+
+    if (!w)
+        return AVERROR(ENOMEM);
+
+    while (*s)
+        if (!isspace(*s++)) *wp++ = s[-1];
+    *wp++ = 0;
+
+    p.class      = &class;
+    p.stack_index=100;
+    p.s= w;
+    p.const_names = const_names;
+    p.funcs1      = funcs1;
+    p.func1_names = func1_names;
+    p.funcs2      = funcs2;
+    p.func2_names = func2_names;
+    p.log_offset = log_offset;
+    p.log_ctx    = log_ctx;
+
+    if ((ret = parse_expr(&e, &p)) < 0)
+        goto end;
+    if (!verify_expr(e)) {
+        av_free_expr(e);
+        ret = AVERROR(EINVAL);
+        goto end;
+    }
+    *expr = e;
+end:
+    av_free(w);
+    return ret;
+}
+
+double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
+{
+    Parser p;
+
+    p.const_values = const_values;
+    p.opaque     = opaque;
+    return eval_expr(&p, e);
+}
+
+int av_parse_and_eval_expr(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);
+
+    if (ret < 0) {
+        *d = NAN;
+        return ret;
+    }
+    *d = av_eval_expr(e, const_values, opaque);
+    av_free_expr(e);
+    return isnan(*d) ? AVERROR(EINVAL) : 0;
+}
+
+#ifdef TEST
+#undef printf
+static double const_values[] = {
+    M_PI,
+    M_E,
+    0
+};
+
+static const char *const_names[] = {
+    "PI",
+    "E",
+    0
+};
+
+int main(void)
+{
+    int i;
+    double d;
+    av_parse_and_eval_expr(&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",
+                           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)",
+                                   const_names, const_values,
+                                   NULL, NULL, NULL, NULL, NULL, 0, NULL);
+        STOP_TIMER("av_parse_and_eval_expr")
+    }
+    return 0;
+}
+#endif
diff --git a/libavutil/eval.h b/libavutil/eval.h
new file mode 100644
index 0000000..3559323
--- /dev/null
+++ b/libavutil/eval.h
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2002 Michael Niedermayer <michaelni at gmx.at>
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * simple arithmetic expression evaluator
+ */
+
+#ifndef AVUTIL_EVAL_H
+#define AVUTIL_EVAL_H
+
+typedef struct AVExpr AVExpr;
+
+/**
+ * Parses and evaluates an expression.
+ * Note, this is significantly slower than av_eval_expr().
+ *
+ * @param res a pointer to a double where is put the result value of
+ *        the expression, or NAN in case of invalid expression
+ * @param s expression as a zero terminated string, for example
+ *        "1+2^3+5*5+sin(2/3)"
+ * @param const_names NULL terminated array of zero terminated strings
+ *        of constant identifers, for example {"PI", "E", 0}
+ * @param const_values a zero terminated array of values for the
+ *        identifers from const_names
+ * @param func1_names NULL terminated array of zero terminated strings
+ *        of funcs1 identifers
+ * @param funcs1 NULL terminated array of function pointers for
+ *        functions which take 1 argument
+ * @param func2_names NULL terminated array of zero terminated strings
+ *        of funcs2 identifers
+ * @param funcs2 NULL terminated array of function pointers for
+ *        functions which take 2 arguments
+ * @param opaque a pointer which will be passed to all functions from
+ *        funcs1 and funcs2
+ * @param log_ctx parent logging context
+ * @return 0 in case of success, a negative value corresponding to an
+ *        AVERROR code otherwise
+ */
+int av_parse_and_eval_expr(double *res, 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);
+
+/**
+ * Parses an expression.
+ *
+ * @param expr a pointer where is put an AVExpr containing the parsed
+ *        value in case of successfull parsing, or NULL otherwise.
+ *        The pointed to AVExpr must be freed with av_free_expr() by
+ *        the user when it is not needed anymore.
+ * @param s expression as a zero terminated string, for example
+ *        "1+2^3+5*5+sin(2/3)"
+ * @param const_names NULL terminated array of zero terminated strings
+ *        of constant identifers, for example {"PI", "E", 0}
+ * @param func1_names NULL terminated array of zero terminated strings
+ *        of funcs1 identifers
+ * @param funcs1 NULL terminated array of function pointers for
+ *        functions which take 1 argument
+ * @param func2_names NULL terminated array of zero terminated strings
+ *        of funcs2 identifers
+ * @param funcs2 NULL terminated array of function pointers for
+ *        functions which take 2 arguments
+ * @param log_ctx parent logging context
+ * @return 0 in case of success, a negative value corresponding to an
+ *        AVERROR code otherwise
+ */
+int av_parse_expr(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);
+
+/**
+ * Evaluates a previously parsed expression.
+ *
+ * @param const_values a zero terminated array of values for the
+ *        identifers from ff_parse() const_names
+ * @param opaque a pointer which will be passed to all functions from
+ *        funcs1 and funcs2
+ * @return the value of the expression
+ */
+double av_eval_expr(AVExpr *e, const double *const_values, void *opaque);
+
+/**
+ * Frees a parsed expression previously created with av_parse_expr().
+ */
+void av_free_expr(AVExpr *e);
+
+/**
+ * Parses the string in numstr and returns its value as a double. If
+ * the string is empty, contains only whitespaces, or does not contain
+ * an initial substring that has the expected syntax for a
+ * floating-point number, no conversion is performed. In this case,
+ * returns a value of zero and the value returned in tail is the value
+ * of numstr.
+ *
+ * @param numstr a string representing a number, may contain one of
+ *        the International System number postfixes, for example 'K',
+ *        'M', 'G'. If 'i' is appended after the postfix, powers of 2
+ *        are used instead of powers of 10. The 'B' postfix multiplies
+ *        the value for 8, and can be appended after another postfix
+ *        or used alone. This allows using for example 'KB', 'MiB',
+ *        'G' and 'B' as postfix.
+ * @param tail if non-NULL puts here the pointer to the char next
+ *        after the last parsed character
+ */
+double av_strtod(const char *numstr, char **tail);
+
+#endif /* AVUTIL_EVAL_H */
-- 
1.7.1




More information about the ffmpeg-devel mailing list