Mercurial > libavcodec.hg
annotate eval.c @ 3770:ea345e1e440f libavcodec
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
Instead, error messages are passed upward by means of a struct member variable.
| author | takis |
|---|---|
| date | Tue, 26 Sep 2006 22:01:33 +0000 |
| parents | 9813c594dac5 |
| children | 699bceae338c |
| rev | line source |
|---|---|
| 612 | 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 | |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 612 | 19 * |
| 20 */ | |
| 21 | |
| 1106 | 22 /** |
| 23 * @file eval.c | |
| 24 * simple arithmetic expression evaluator. | |
| 25 * | |
| 612 | 26 * see http://joe.hotchkiss.com/programming/eval/eval.html |
| 27 */ | |
| 28 | |
| 1057 | 29 #include "avcodec.h" |
| 30 #include "mpegvideo.h" | |
| 31 | |
| 612 | 32 #include <stdio.h> |
| 33 #include <stdlib.h> | |
| 34 #include <string.h> | |
| 35 #include <math.h> | |
| 36 | |
|
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
37 #ifndef NAN |
| 3753 | 38 #define NAN 0.0/0.0 |
|
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
39 #endif |
|
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
40 |
| 627 | 41 #ifndef M_PI |
| 42 #define M_PI 3.14159265358979323846 | |
| 43 #endif | |
| 44 | |
| 612 | 45 typedef struct Parser{ |
| 46 int stack_index; | |
| 47 char *s; | |
| 48 double *const_value; | |
| 1057 | 49 const char **const_name; // NULL terminated |
| 612 | 50 double (**func1)(void *, double a); // NULL terminated |
| 1057 | 51 const char **func1_name; // NULL terminated |
| 612 | 52 double (**func2)(void *, double a, double b); // NULL terminated |
| 53 char **func2_name; // NULL terminated | |
| 54 void *opaque; | |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
55 char **error; |
| 612 | 56 } Parser; |
| 57 | |
| 3756 | 58 extern double av_strtod(const char *name, char **tail); |
| 59 | |
| 2434 | 60 static double evalExpression(Parser *p); |
| 612 | 61 |
| 1057 | 62 static int strmatch(const char *s, const char *prefix){ |
| 612 | 63 int i; |
| 64 for(i=0; prefix[i]; i++){ | |
| 65 if(prefix[i] != s[i]) return 0; | |
| 66 } | |
| 67 return 1; | |
| 68 } | |
| 69 | |
| 2434 | 70 static double evalPrimary(Parser *p){ |
| 612 | 71 double d, d2=NAN; |
| 72 char *next= p->s; | |
| 73 int i; | |
| 74 | |
| 75 /* number */ | |
|
3731
8b8773577dd9
Add support for SI (k, M, ...) and IEC/IEEE (Ki, Mi, ...) units.
takis
parents:
3730
diff
changeset
|
76 d= av_strtod(p->s, &next); |
| 612 | 77 if(next != p->s){ |
| 78 p->s= next; | |
| 2434 | 79 return d; |
| 612 | 80 } |
| 2967 | 81 |
| 612 | 82 /* named constants */ |
| 2433 | 83 for(i=0; p->const_name && p->const_name[i]; i++){ |
| 612 | 84 if(strmatch(p->s, p->const_name[i])){ |
| 85 p->s+= strlen(p->const_name[i]); | |
| 2434 | 86 return p->const_value[i]; |
| 612 | 87 } |
| 88 } | |
| 2967 | 89 |
| 612 | 90 p->s= strchr(p->s, '('); |
| 91 if(p->s==NULL){ | |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
92 *p->error = "missing ("; |
| 3754 | 93 p->s= next; |
| 2434 | 94 return NAN; |
| 612 | 95 } |
| 96 p->s++; // "(" | |
| 2434 | 97 d= evalExpression(p); |
| 1815 | 98 if(p->s[0]== ','){ |
| 99 p->s++; // "," | |
| 2434 | 100 d2= evalExpression(p); |
| 612 | 101 } |
| 1815 | 102 if(p->s[0] != ')'){ |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
103 *p->error = "missing )"; |
| 2434 | 104 return NAN; |
| 1815 | 105 } |
| 106 p->s++; // ")" | |
| 2967 | 107 |
| 612 | 108 if( strmatch(next, "sinh" ) ) d= sinh(d); |
| 109 else if( strmatch(next, "cosh" ) ) d= cosh(d); | |
| 110 else if( strmatch(next, "tanh" ) ) d= tanh(d); | |
| 111 else if( strmatch(next, "sin" ) ) d= sin(d); | |
| 112 else if( strmatch(next, "cos" ) ) d= cos(d); | |
| 113 else if( strmatch(next, "tan" ) ) d= tan(d); | |
| 114 else if( strmatch(next, "exp" ) ) d= exp(d); | |
| 115 else if( strmatch(next, "log" ) ) d= log(d); | |
| 116 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d)); | |
| 117 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI); | |
| 1057 | 118 else if( strmatch(next, "abs" ) ) d= fabs(d); |
| 3755 | 119 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2; |
| 120 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2; | |
| 121 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; | |
| 122 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0; | |
| 123 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; | |
| 124 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; | |
| 612 | 125 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; |
| 2434 | 126 else if( strmatch(next, "(" ) ) d= d; |
| 612 | 127 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); |
| 128 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; | |
| 129 else{ | |
| 130 for(i=0; p->func1_name && p->func1_name[i]; i++){ | |
| 131 if(strmatch(next, p->func1_name[i])){ | |
| 2434 | 132 return p->func1[i](p->opaque, d); |
| 612 | 133 } |
| 134 } | |
| 135 | |
| 136 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
| 137 if(strmatch(next, p->func2_name[i])){ | |
| 2434 | 138 return p->func2[i](p->opaque, d, d2); |
| 612 | 139 } |
| 140 } | |
| 141 | |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
142 *p->error = "unknown function"; |
| 2434 | 143 return NAN; |
| 612 | 144 } |
| 2433 | 145 |
| 2434 | 146 return d; |
| 2967 | 147 } |
| 2436 | 148 |
| 2434 | 149 static double evalPow(Parser *p){ |
| 2436 | 150 int sign= (*p->s == '+') - (*p->s == '-'); |
| 151 p->s += sign&1; | |
| 152 return (sign|1) * evalPrimary(p); | |
| 2434 | 153 } |
| 612 | 154 |
| 2434 | 155 static double evalFactor(Parser *p){ |
| 156 double ret= evalPow(p); | |
| 157 while(p->s[0]=='^'){ | |
| 612 | 158 p->s++; |
| 2434 | 159 ret= pow(ret, evalPow(p)); |
| 612 | 160 } |
| 2434 | 161 return ret; |
| 612 | 162 } |
| 163 | |
| 2434 | 164 static double evalTerm(Parser *p){ |
| 165 double ret= evalFactor(p); | |
| 166 while(p->s[0]=='*' || p->s[0]=='/'){ | |
| 167 if(*p->s++ == '*') ret*= evalFactor(p); | |
| 168 else ret/= evalFactor(p); | |
| 612 | 169 } |
| 2434 | 170 return ret; |
| 612 | 171 } |
| 172 | |
| 2434 | 173 static double evalExpression(Parser *p){ |
| 2436 | 174 double ret= 0; |
| 612 | 175 |
| 2434 | 176 if(p->stack_index <= 0) //protect against stack overflows |
| 177 return NAN; | |
| 178 p->stack_index--; | |
| 179 | |
| 2436 | 180 do{ |
| 181 ret += evalTerm(p); | |
| 182 }while(*p->s == '+' || *p->s == '-'); | |
| 612 | 183 |
| 2434 | 184 p->stack_index++; |
| 612 | 185 |
| 2434 | 186 return ret; |
| 612 | 187 } |
| 188 | |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
189 double ff_eval2(char *s, double *const_value, const char **const_name, |
| 1057 | 190 double (**func1)(void *, double), const char **func1_name, |
| 612 | 191 double (**func2)(void *, double, double), char **func2_name, |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
192 void *opaque, char **error){ |
| 612 | 193 Parser p; |
| 2967 | 194 |
| 2434 | 195 p.stack_index=100; |
| 612 | 196 p.s= s; |
| 197 p.const_value= const_value; | |
| 198 p.const_name = const_name; | |
| 199 p.func1 = func1; | |
| 200 p.func1_name = func1_name; | |
| 201 p.func2 = func2; | |
| 202 p.func2_name = func2_name; | |
| 203 p.opaque = opaque; | |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
204 p.error= error; |
| 2967 | 205 |
| 2434 | 206 return evalExpression(&p); |
| 612 | 207 } |
| 2433 | 208 |
|
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
209 #if LIBAVCODEC_VERSION_INT < ((51<<16)+(16<<8)+0) |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
210 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name, |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
211 double (**func1)(void *, double), const char **func1_name, |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
212 double (**func2)(void *, double, double), char **func2_name, |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
213 void *opaque){ |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
214 char *error=NULL; |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
215 double ret; |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
216 ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error); |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
217 if (error) |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
218 av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error); |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
219 return ret; |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
220 } |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
221 #endif |
|
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
222 |
| 2433 | 223 #ifdef TEST |
| 2967 | 224 #undef printf |
| 2433 | 225 static double const_values[]={ |
| 226 M_PI, | |
| 227 M_E, | |
| 228 0 | |
| 229 }; | |
| 230 static const char *const_names[]={ | |
| 231 "PI", | |
| 232 "E", | |
| 233 0 | |
| 234 }; | |
| 235 main(){ | |
| 2436 | 236 int i; |
| 2433 | 237 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)); |
| 3730 | 238 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL)); |
| 2967 | 239 |
| 2436 | 240 for(i=0; i<1050; i++){ |
| 241 START_TIMER | |
| 242 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); | |
| 243 STOP_TIMER("ff_eval") | |
| 244 } | |
| 2433 | 245 } |
| 246 #endif |
