]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/eval.c
segfault fix
[frescor/ffmpeg.git] / libavcodec / eval.c
1 /*
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  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21
22 /**
23  * @file eval.c
24  * simple arithmetic expression evaluator.
25  *
26  * see http://joe.hotchkiss.com/programming/eval/eval.html
27  */
28
29 #include "avcodec.h"
30 #include "mpegvideo.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #ifndef NAN
38   #define NAN 0.0/0.0
39 #endif
40
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846
43 #endif
44
45 typedef struct Parser{
46     int stack_index;
47     char *s;
48     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     double (**func2)(void *, double a, double b); // NULL terminated
53     char **func2_name;          // NULL terminated
54     void *opaque;
55 } Parser;
56
57 static double evalExpression(Parser *p);
58
59 static int strmatch(const char *s, const char *prefix){
60     int i;
61     for(i=0; prefix[i]; i++){
62         if(prefix[i] != s[i]) return 0;
63     }
64     return 1;
65 }
66
67 static double evalPrimary(Parser *p){
68     double d, d2=NAN;
69     char *next= p->s;
70     int i;
71
72     /* number */
73     d= av_strtod(p->s, &next);
74     if(next != p->s){
75         p->s= next;
76         return d;
77     }
78
79     /* named constants */
80     for(i=0; p->const_name && p->const_name[i]; i++){
81         if(strmatch(p->s, p->const_name[i])){
82             p->s+= strlen(p->const_name[i]);
83             return p->const_value[i];
84         }
85     }
86
87     p->s= strchr(p->s, '(');
88     if(p->s==NULL){
89         av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
90         p->s= next;
91         return NAN;
92     }
93     p->s++; // "("
94     d= evalExpression(p);
95     if(p->s[0]== ','){
96         p->s++; // ","
97         d2= evalExpression(p);
98     }
99     if(p->s[0] != ')'){
100         av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
101         return NAN;
102     }
103     p->s++; // ")"
104
105          if( strmatch(next, "sinh"  ) ) d= sinh(d);
106     else if( strmatch(next, "cosh"  ) ) d= cosh(d);
107     else if( strmatch(next, "tanh"  ) ) d= tanh(d);
108     else if( strmatch(next, "sin"   ) ) d= sin(d);
109     else if( strmatch(next, "cos"   ) ) d= cos(d);
110     else if( strmatch(next, "tan"   ) ) d= tan(d);
111     else if( strmatch(next, "exp"   ) ) d= exp(d);
112     else if( strmatch(next, "log"   ) ) d= log(d);
113     else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
114     else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
115     else if( strmatch(next, "abs"   ) ) d= fabs(d);
116     else if( strmatch(next, "max"   ) ) d= d > d2 ? d : d2;
117     else if( strmatch(next, "min"   ) ) d= d < d2 ? d : d2;
118     else if( strmatch(next, "gt"    ) ) d= d > d2 ? 1.0 : 0.0;
119     else if( strmatch(next, "gte"    ) ) d= d >= d2 ? 1.0 : 0.0;
120     else if( strmatch(next, "lt"    ) ) d= d > d2 ? 0.0 : 1.0;
121     else if( strmatch(next, "lte"    ) ) d= d >= d2 ? 0.0 : 1.0;
122     else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
123     else if( strmatch(next, "("     ) ) d= d;
124 //    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
125 //    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
126     else{
127         for(i=0; p->func1_name && p->func1_name[i]; i++){
128             if(strmatch(next, p->func1_name[i])){
129                 return p->func1[i](p->opaque, d);
130             }
131         }
132
133         for(i=0; p->func2_name && p->func2_name[i]; i++){
134             if(strmatch(next, p->func2_name[i])){
135                 return p->func2[i](p->opaque, d, d2);
136             }
137         }
138
139         av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
140         return NAN;
141     }
142
143     return d;
144 }
145
146 static double evalPow(Parser *p){
147     int sign= (*p->s == '+') - (*p->s == '-');
148     p->s += sign&1;
149     return (sign|1) * evalPrimary(p);
150 }
151
152 static double evalFactor(Parser *p){
153     double ret= evalPow(p);
154     while(p->s[0]=='^'){
155         p->s++;
156         ret= pow(ret, evalPow(p));
157     }
158     return ret;
159 }
160
161 static double evalTerm(Parser *p){
162     double ret= evalFactor(p);
163     while(p->s[0]=='*' || p->s[0]=='/'){
164         if(*p->s++ == '*') ret*= evalFactor(p);
165         else               ret/= evalFactor(p);
166     }
167     return ret;
168 }
169
170 static double evalExpression(Parser *p){
171     double ret= 0;
172
173     if(p->stack_index <= 0) //protect against stack overflows
174         return NAN;
175     p->stack_index--;
176
177     do{
178         ret += evalTerm(p);
179     }while(*p->s == '+' || *p->s == '-');
180
181     p->stack_index++;
182
183     return ret;
184 }
185
186 double ff_eval(char *s, double *const_value, const char **const_name,
187                double (**func1)(void *, double), const char **func1_name,
188                double (**func2)(void *, double, double), char **func2_name,
189                void *opaque){
190     Parser p;
191
192     p.stack_index=100;
193     p.s= s;
194     p.const_value= const_value;
195     p.const_name = const_name;
196     p.func1      = func1;
197     p.func1_name = func1_name;
198     p.func2      = func2;
199     p.func2_name = func2_name;
200     p.opaque     = opaque;
201
202     return evalExpression(&p);
203 }
204
205 #ifdef TEST
206 #undef printf
207 static double const_values[]={
208     M_PI,
209     M_E,
210     0
211 };
212 static const char *const_names[]={
213     "PI",
214     "E",
215     0
216 };
217 main(){
218     int i;
219     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));
220     printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
221
222     for(i=0; i<1050; i++){
223         START_TIMER
224             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);
225         STOP_TIMER("ff_eval")
226     }
227 }
228 #endif