2 * simple arithmetic expression evaluator
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
24 * simple arithmetic expression evaluator.
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
30 #include "mpegvideo.h"
42 #define M_PI 3.14159265358979323846
45 typedef struct Parser{
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
58 static double evalExpression(Parser *p);
60 static int8_t si_prefixes['z' - 'E' + 1]={
83 /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
84 * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
85 * function assumes that the unit of numbers is bits not bytes.
87 static double av_strtod(const char *name, char **tail) {
91 d = strtod(name, &next);
92 /* if parsing succeeded, check for and interpret postfixes */
95 if(*next >= 'E' && *next <= 'z'){
96 int e= si_prefixes[*next - 'E'];
113 /* if requested, fill in tail with the position after the last parsed
120 static int strmatch(const char *s, const char *prefix){
122 for(i=0; prefix[i]; i++){
123 if(prefix[i] != s[i]) return 0;
128 static double evalPrimary(Parser *p){
134 d= av_strtod(p->s, &next);
140 /* named constants */
141 for(i=0; p->const_name && p->const_name[i]; i++){
142 if(strmatch(p->s, p->const_name[i])){
143 p->s+= strlen(p->const_name[i]);
144 return p->const_value[i];
148 p->s= strchr(p->s, '(');
150 *p->error = "missing (";
155 d= evalExpression(p);
158 d2= evalExpression(p);
161 *p->error = "missing )";
166 if( strmatch(next, "sinh" ) ) d= sinh(d);
167 else if( strmatch(next, "cosh" ) ) d= cosh(d);
168 else if( strmatch(next, "tanh" ) ) d= tanh(d);
169 else if( strmatch(next, "sin" ) ) d= sin(d);
170 else if( strmatch(next, "cos" ) ) d= cos(d);
171 else if( strmatch(next, "tan" ) ) d= tan(d);
172 else if( strmatch(next, "exp" ) ) d= exp(d);
173 else if( strmatch(next, "log" ) ) d= log(d);
174 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
175 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
176 else if( strmatch(next, "abs" ) ) d= fabs(d);
177 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
178 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
179 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
180 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
181 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
182 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
183 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
184 else if( strmatch(next, "(" ) ) d= d;
185 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
186 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
188 for(i=0; p->func1_name && p->func1_name[i]; i++){
189 if(strmatch(next, p->func1_name[i])){
190 return p->func1[i](p->opaque, d);
194 for(i=0; p->func2_name && p->func2_name[i]; i++){
195 if(strmatch(next, p->func2_name[i])){
196 return p->func2[i](p->opaque, d, d2);
200 *p->error = "unknown function";
207 static double evalPow(Parser *p){
208 int sign= (*p->s == '+') - (*p->s == '-');
210 return (sign|1) * evalPrimary(p);
213 static double evalFactor(Parser *p){
214 double ret= evalPow(p);
217 ret= pow(ret, evalPow(p));
222 static double evalTerm(Parser *p){
223 double ret= evalFactor(p);
224 while(p->s[0]=='*' || p->s[0]=='/'){
225 if(*p->s++ == '*') ret*= evalFactor(p);
226 else ret/= evalFactor(p);
231 static double evalExpression(Parser *p){
234 if(p->stack_index <= 0) //protect against stack overflows
240 }while(*p->s == '+' || *p->s == '-');
247 double ff_eval2(char *s, double *const_value, const char **const_name,
248 double (**func1)(void *, double), const char **func1_name,
249 double (**func2)(void *, double, double), char **func2_name,
250 void *opaque, char **error){
255 p.const_value= const_value;
256 p.const_name = const_name;
258 p.func1_name = func1_name;
260 p.func2_name = func2_name;
264 return evalExpression(&p);
267 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
268 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
269 double (**func1)(void *, double), const char **func1_name,
270 double (**func2)(void *, double, double), char **func2_name,
274 ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
276 av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
283 static double const_values[]={
288 static const char *const_names[]={
295 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));
296 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
298 for(i=0; i<1050; i++){
300 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);
301 STOP_TIMER("ff_eval")