eval.c 6.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * simple arithmetic expression evaluator
 *
 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

Michael Niedermayer's avatar
Michael Niedermayer committed
22 23 24 25
/**
 * @file eval.c
 * simple arithmetic expression evaluator.
 *
26 27 28
 * see http://joe.hotchkiss.com/programming/eval/eval.html
 */

29 30 31
#include "avcodec.h"
#include "mpegvideo.h"

32 33 34 35 36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

37 38 39 40
#ifndef NAN
  #define NAN 0
#endif

41 42 43 44
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

45 46 47 48
typedef struct Parser{
    int stack_index;
    char *s;
    double *const_value;
49
    const char **const_name;          // NULL terminated
50
    double (**func1)(void *, double a); // NULL terminated
51
    const char **func1_name;          // NULL terminated
52 53 54 55 56
    double (**func2)(void *, double a, double b); // NULL terminated
    char **func2_name;          // NULL terminated
    void *opaque;
} Parser;

Michael Niedermayer's avatar
Michael Niedermayer committed
57
static double evalExpression(Parser *p);
58

59
static int strmatch(const char *s, const char *prefix){
60 61 62 63 64 65 66
    int i;
    for(i=0; prefix[i]; i++){
        if(prefix[i] != s[i]) return 0;
    }
    return 1;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
67
static double evalPrimary(Parser *p){
68 69 70 71 72 73 74 75
    double d, d2=NAN;
    char *next= p->s;
    int i;

    /* number */
    d= strtod(p->s, &next);
    if(next != p->s){
        p->s= next;
Michael Niedermayer's avatar
Michael Niedermayer committed
76
        return d;
77 78 79
    }
    
    /* named constants */
80
    for(i=0; p->const_name && p->const_name[i]; i++){
81 82
        if(strmatch(p->s, p->const_name[i])){
            p->s+= strlen(p->const_name[i]);
Michael Niedermayer's avatar
Michael Niedermayer committed
83
            return p->const_value[i];
84 85 86 87 88
        }
    }
    
    p->s= strchr(p->s, '(');
    if(p->s==NULL){
89
        av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
Michael Niedermayer's avatar
Michael Niedermayer committed
90
        return NAN;
91 92
    }
    p->s++; // "("
Michael Niedermayer's avatar
Michael Niedermayer committed
93
    d= evalExpression(p);
94 95
    if(p->s[0]== ','){
        p->s++; // ","
Michael Niedermayer's avatar
Michael Niedermayer committed
96
        d2= evalExpression(p);
97
    }
98 99
    if(p->s[0] != ')'){
        av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
Michael Niedermayer's avatar
Michael Niedermayer committed
100
        return NAN;
101 102
    }
    p->s++; // ")"
103 104 105 106 107 108 109 110 111 112 113
    
         if( strmatch(next, "sinh"  ) ) d= sinh(d);
    else if( strmatch(next, "cosh"  ) ) d= cosh(d);
    else if( strmatch(next, "tanh"  ) ) d= tanh(d);
    else if( strmatch(next, "sin"   ) ) d= sin(d);
    else if( strmatch(next, "cos"   ) ) d= cos(d);
    else if( strmatch(next, "tan"   ) ) d= tan(d);
    else if( strmatch(next, "exp"   ) ) d= exp(d);
    else if( strmatch(next, "log"   ) ) d= log(d);
    else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
    else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
114
    else if( strmatch(next, "abs"   ) ) d= fabs(d);
115 116 117
    else if( strmatch(next, "max"   ) ) d= d > d2 ? d : d2;
    else if( strmatch(next, "min"   ) ) d= d < d2 ? d : d2;
    else if( strmatch(next, "gt"    ) ) d= d > d2 ? 1.0 : 0.0;
118
    else if( strmatch(next, "gte"    ) ) d= d >= d2 ? 1.0 : 0.0;
119
    else if( strmatch(next, "lt"    ) ) d= d > d2 ? 0.0 : 1.0;
120
    else if( strmatch(next, "lte"    ) ) d= d >= d2 ? 0.0 : 1.0;
121
    else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
Michael Niedermayer's avatar
Michael Niedermayer committed
122
    else if( strmatch(next, "("     ) ) d= d;
123 124 125 126 127
//    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
//    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
    else{
        for(i=0; p->func1_name && p->func1_name[i]; i++){
            if(strmatch(next, p->func1_name[i])){
Michael Niedermayer's avatar
Michael Niedermayer committed
128
                return p->func1[i](p->opaque, d);
129 130 131 132 133
            }
        }

        for(i=0; p->func2_name && p->func2_name[i]; i++){
            if(strmatch(next, p->func2_name[i])){
Michael Niedermayer's avatar
Michael Niedermayer committed
134
                return p->func2[i](p->opaque, d, d2);
135 136 137
            }
        }

138
        av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
Michael Niedermayer's avatar
Michael Niedermayer committed
139
        return NAN;
140
    }
141

Michael Niedermayer's avatar
Michael Niedermayer committed
142
    return d;
143
}      
Michael Niedermayer's avatar
Michael Niedermayer committed
144

Michael Niedermayer's avatar
Michael Niedermayer committed
145
static double evalPow(Parser *p){
Michael Niedermayer's avatar
Michael Niedermayer committed
146 147 148
    int sign= (*p->s == '+') - (*p->s == '-');
    p->s += sign&1;
    return (sign|1) * evalPrimary(p);
149 150
}

Michael Niedermayer's avatar
Michael Niedermayer committed
151 152
static double evalFactor(Parser *p){
    double ret= evalPow(p);
153 154
    while(p->s[0]=='^'){
        p->s++;
Michael Niedermayer's avatar
Michael Niedermayer committed
155
        ret= pow(ret, evalPow(p));
156
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
157
    return ret;
158 159
}

Michael Niedermayer's avatar
Michael Niedermayer committed
160 161
static double evalTerm(Parser *p){
    double ret= evalFactor(p);
162
    while(p->s[0]=='*' || p->s[0]=='/'){
Michael Niedermayer's avatar
Michael Niedermayer committed
163 164
        if(*p->s++ == '*') ret*= evalFactor(p);
        else               ret/= evalFactor(p);
165
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
166
    return ret;
167 168
}

Michael Niedermayer's avatar
Michael Niedermayer committed
169
static double evalExpression(Parser *p){
Michael Niedermayer's avatar
Michael Niedermayer committed
170
    double ret= 0;
171

Michael Niedermayer's avatar
Michael Niedermayer committed
172 173 174 175
    if(p->stack_index <= 0) //protect against stack overflows
        return NAN;
    p->stack_index--;

Michael Niedermayer's avatar
Michael Niedermayer committed
176 177 178
    do{
        ret += evalTerm(p);
    }while(*p->s == '+' || *p->s == '-');
Michael Niedermayer's avatar
Michael Niedermayer committed
179 180 181 182

    p->stack_index++;

    return ret;
183 184
}

185 186
double ff_eval(char *s, double *const_value, const char **const_name,
               double (**func1)(void *, double), const char **func1_name,
187 188 189 190
               double (**func2)(void *, double, double), char **func2_name,
               void *opaque){
    Parser p;
    
Michael Niedermayer's avatar
Michael Niedermayer committed
191
    p.stack_index=100;
192 193 194 195 196 197 198 199 200
    p.s= s;
    p.const_value= const_value;
    p.const_name = const_name;
    p.func1      = func1;
    p.func1_name = func1_name;
    p.func2      = func2;
    p.func2_name = func2_name;
    p.opaque     = opaque;
    
Michael Niedermayer's avatar
Michael Niedermayer committed
201
    return evalExpression(&p);
202
}
203 204 205 206 207 208 209 210 211 212 213 214 215 216

#ifdef TEST
#undef printf 
static double const_values[]={
    M_PI,
    M_E,
    0
};
static const char *const_names[]={
    "PI",
    "E",
    0
};
main(){
Michael Niedermayer's avatar
Michael Niedermayer committed
217
    int i;
218
    printf("%f == 12.7\n", ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
Michael Niedermayer's avatar
Michael Niedermayer committed
219 220 221 222 223 224
    
    for(i=0; i<1050; i++){
        START_TIMER
            ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL);
        STOP_TIMER("ff_eval")
    }
225 226
}
#endif