Statistics
| Branch: | Revision:

ffmpeg / libavcodec / eval.c @ 1449b722

History | View | Annotate | Download (6.12 KB)

1 3aa102be Michael Niedermayer
/*
2
 * simple arithmetic expression evaluator
3
 *
4
 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 3aa102be Michael Niedermayer
 *
20
 */
21
22 983e3246 Michael Niedermayer
/**
23
 * @file eval.c
24
 * simple arithmetic expression evaluator.
25
 *
26 3aa102be Michael Niedermayer
 * see http://joe.hotchkiss.com/programming/eval/eval.html
27
 */
28
29 5c91a675 Zdenek Kabelac
#include "avcodec.h"
30
#include "mpegvideo.h"
31
32 3aa102be Michael Niedermayer
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <math.h>
36
37 d0d816d8 RĂ©mi Guyomarch
#ifndef NAN
38
  #define NAN 0
39
#endif
40
41 471d7dc3 Michael Niedermayer
#ifndef M_PI
42
#define M_PI 3.14159265358979323846
43
#endif
44
45 3aa102be Michael Niedermayer
typedef struct Parser{
46
    int stack_index;
47
    char *s;
48
    double *const_value;
49 5c91a675 Zdenek Kabelac
    const char **const_name;          // NULL terminated
50 3aa102be Michael Niedermayer
    double (**func1)(void *, double a); // NULL terminated
51 5c91a675 Zdenek Kabelac
    const char **func1_name;          // NULL terminated
52 3aa102be Michael Niedermayer
    double (**func2)(void *, double a, double b); // NULL terminated
53
    char **func2_name;          // NULL terminated
54
    void *opaque;
55
} Parser;
56
57 67e11730 Michael Niedermayer
static double evalExpression(Parser *p);
58 3aa102be Michael Niedermayer
59 5c91a675 Zdenek Kabelac
static int strmatch(const char *s, const char *prefix){
60 3aa102be Michael Niedermayer
    int i;
61
    for(i=0; prefix[i]; i++){
62
        if(prefix[i] != s[i]) return 0;
63
    }
64
    return 1;
65
}
66
67 67e11730 Michael Niedermayer
static double evalPrimary(Parser *p){
68 3aa102be Michael Niedermayer
    double d, d2=NAN;
69
    char *next= p->s;
70
    int i;
71
72
    /* number */
73
    d= strtod(p->s, &next);
74
    if(next != p->s){
75
        p->s= next;
76 67e11730 Michael Niedermayer
        return d;
77 3aa102be Michael Niedermayer
    }
78 115329f1 Diego Biurrun
79 3aa102be Michael Niedermayer
    /* named constants */
80 b349fde1 Michael Niedermayer
    for(i=0; p->const_name && p->const_name[i]; i++){
81 3aa102be Michael Niedermayer
        if(strmatch(p->s, p->const_name[i])){
82
            p->s+= strlen(p->const_name[i]);
83 67e11730 Michael Niedermayer
            return p->const_value[i];
84 3aa102be Michael Niedermayer
        }
85
    }
86 115329f1 Diego Biurrun
87 3aa102be Michael Niedermayer
    p->s= strchr(p->s, '(');
88
    if(p->s==NULL){
89 9b879566 Michel Bardiaux
        av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
90 67e11730 Michael Niedermayer
        return NAN;
91 3aa102be Michael Niedermayer
    }
92
    p->s++; // "("
93 67e11730 Michael Niedermayer
    d= evalExpression(p);
94 69f5de18 Alex Beregszaszi
    if(p->s[0]== ','){
95
        p->s++; // ","
96 67e11730 Michael Niedermayer
        d2= evalExpression(p);
97 3aa102be Michael Niedermayer
    }
98 69f5de18 Alex Beregszaszi
    if(p->s[0] != ')'){
99
        av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
100 67e11730 Michael Niedermayer
        return NAN;
101 69f5de18 Alex Beregszaszi
    }
102
    p->s++; // ")"
103 115329f1 Diego Biurrun
104 3aa102be Michael Niedermayer
         if( strmatch(next, "sinh"  ) ) d= sinh(d);
105
    else if( strmatch(next, "cosh"  ) ) d= cosh(d);
106
    else if( strmatch(next, "tanh"  ) ) d= tanh(d);
107
    else if( strmatch(next, "sin"   ) ) d= sin(d);
108
    else if( strmatch(next, "cos"   ) ) d= cos(d);
109
    else if( strmatch(next, "tan"   ) ) d= tan(d);
110
    else if( strmatch(next, "exp"   ) ) d= exp(d);
111
    else if( strmatch(next, "log"   ) ) d= log(d);
112
    else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
113
    else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
114 5c91a675 Zdenek Kabelac
    else if( strmatch(next, "abs"   ) ) d= fabs(d);
115 3aa102be Michael Niedermayer
    else if( strmatch(next, "max"   ) ) d= d > d2 ? d : d2;
116
    else if( strmatch(next, "min"   ) ) d= d < d2 ? d : d2;
117
    else if( strmatch(next, "gt"    ) ) d= d > d2 ? 1.0 : 0.0;
118 69f5de18 Alex Beregszaszi
    else if( strmatch(next, "gte"    ) ) d= d >= d2 ? 1.0 : 0.0;
119 3aa102be Michael Niedermayer
    else if( strmatch(next, "lt"    ) ) d= d > d2 ? 0.0 : 1.0;
120 69f5de18 Alex Beregszaszi
    else if( strmatch(next, "lte"    ) ) d= d >= d2 ? 0.0 : 1.0;
121 3aa102be Michael Niedermayer
    else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
122 67e11730 Michael Niedermayer
    else if( strmatch(next, "("     ) ) d= d;
123 3aa102be Michael Niedermayer
//    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
124
//    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
125
    else{
126
        for(i=0; p->func1_name && p->func1_name[i]; i++){
127
            if(strmatch(next, p->func1_name[i])){
128 67e11730 Michael Niedermayer
                return p->func1[i](p->opaque, d);
129 3aa102be Michael Niedermayer
            }
130
        }
131
132
        for(i=0; p->func2_name && p->func2_name[i]; i++){
133
            if(strmatch(next, p->func2_name[i])){
134 67e11730 Michael Niedermayer
                return p->func2[i](p->opaque, d, d2);
135 3aa102be Michael Niedermayer
            }
136
        }
137
138 b349fde1 Michael Niedermayer
        av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
139 67e11730 Michael Niedermayer
        return NAN;
140 3aa102be Michael Niedermayer
    }
141 b349fde1 Michael Niedermayer
142 67e11730 Michael Niedermayer
    return d;
143 115329f1 Diego Biurrun
}
144 80a49958 Michael Niedermayer
145 67e11730 Michael Niedermayer
static double evalPow(Parser *p){
146 80a49958 Michael Niedermayer
    int sign= (*p->s == '+') - (*p->s == '-');
147
    p->s += sign&1;
148
    return (sign|1) * evalPrimary(p);
149 3aa102be Michael Niedermayer
}
150
151 67e11730 Michael Niedermayer
static double evalFactor(Parser *p){
152
    double ret= evalPow(p);
153 3aa102be Michael Niedermayer
    while(p->s[0]=='^'){
154
        p->s++;
155 67e11730 Michael Niedermayer
        ret= pow(ret, evalPow(p));
156 3aa102be Michael Niedermayer
    }
157 67e11730 Michael Niedermayer
    return ret;
158 3aa102be Michael Niedermayer
}
159
160 67e11730 Michael Niedermayer
static double evalTerm(Parser *p){
161
    double ret= evalFactor(p);
162 3aa102be Michael Niedermayer
    while(p->s[0]=='*' || p->s[0]=='/'){
163 67e11730 Michael Niedermayer
        if(*p->s++ == '*') ret*= evalFactor(p);
164
        else               ret/= evalFactor(p);
165 3aa102be Michael Niedermayer
    }
166 67e11730 Michael Niedermayer
    return ret;
167 3aa102be Michael Niedermayer
}
168
169 67e11730 Michael Niedermayer
static double evalExpression(Parser *p){
170 80a49958 Michael Niedermayer
    double ret= 0;
171 3aa102be Michael Niedermayer
172 67e11730 Michael Niedermayer
    if(p->stack_index <= 0) //protect against stack overflows
173
        return NAN;
174
    p->stack_index--;
175
176 80a49958 Michael Niedermayer
    do{
177
        ret += evalTerm(p);
178
    }while(*p->s == '+' || *p->s == '-');
179 67e11730 Michael Niedermayer
180
    p->stack_index++;
181
182
    return ret;
183 3aa102be Michael Niedermayer
}
184
185 5c91a675 Zdenek Kabelac
double ff_eval(char *s, double *const_value, const char **const_name,
186
               double (**func1)(void *, double), const char **func1_name,
187 3aa102be Michael Niedermayer
               double (**func2)(void *, double, double), char **func2_name,
188
               void *opaque){
189
    Parser p;
190 115329f1 Diego Biurrun
191 67e11730 Michael Niedermayer
    p.stack_index=100;
192 3aa102be Michael Niedermayer
    p.s= s;
193
    p.const_value= const_value;
194
    p.const_name = const_name;
195
    p.func1      = func1;
196
    p.func1_name = func1_name;
197
    p.func2      = func2;
198
    p.func2_name = func2_name;
199
    p.opaque     = opaque;
200 115329f1 Diego Biurrun
201 67e11730 Michael Niedermayer
    return evalExpression(&p);
202 3aa102be Michael Niedermayer
}
203 b349fde1 Michael Niedermayer
204
#ifdef TEST
205 115329f1 Diego Biurrun
#undef printf
206 b349fde1 Michael Niedermayer
static double const_values[]={
207
    M_PI,
208
    M_E,
209
    0
210
};
211
static const char *const_names[]={
212
    "PI",
213
    "E",
214
    0
215
};
216
main(){
217 80a49958 Michael Niedermayer
    int i;
218 b349fde1 Michael Niedermayer
    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));
219 115329f1 Diego Biurrun
220 80a49958 Michael Niedermayer
    for(i=0; i<1050; i++){
221
        START_TIMER
222
            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);
223
        STOP_TIMER("ff_eval")
224
    }
225 b349fde1 Michael Niedermayer
}
226
#endif