Mercurial > libavcodec.hg
annotate eval.c @ 3729:58483364f021 libavcodec
support SI and some non SI prefixes
author | michael |
---|---|
date | Sun, 17 Sep 2006 08:52:41 +0000 |
parents | 0b546eab515d |
children | aae4aed137ea |
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 (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
37 #ifndef NAN |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
38 #define NAN 0 |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
39 #endif |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi 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; | |
55 } Parser; | |
56 | |
2434 | 57 static double evalExpression(Parser *p); |
612 | 58 |
1057 | 59 static int strmatch(const char *s, const char *prefix){ |
612 | 60 int i; |
61 for(i=0; prefix[i]; i++){ | |
62 if(prefix[i] != s[i]) return 0; | |
63 } | |
64 return 1; | |
65 } | |
66 | |
3729 | 67 static int8_t si_prefixes['z' - 'E' + 1]={ |
68 ['y'-'E']= -24, | |
69 ['z'-'E']= -21, | |
70 ['a'-'E']= -18, | |
71 ['f'-'E']= -15, | |
72 ['p'-'E']= -12, | |
73 ['n'-'E']= - 9, | |
74 ['u'-'E']= - 6, | |
75 ['m'-'E']= - 3, | |
76 ['c'-'E']= - 2, | |
77 ['d'-'E']= - 1, | |
78 ['h'-'E']= 2, | |
79 ['k'-'E']= 3, | |
80 ['M'-'E']= 6, | |
81 ['G'-'E']= 9, | |
82 ['T'-'E']= 12, | |
83 ['P'-'E']= 15, | |
84 ['E'-'E']= 18, | |
85 ['Z'-'E']= 21, | |
86 ['Y'-'E']= 24, | |
87 }; | |
88 | |
2434 | 89 static double evalPrimary(Parser *p){ |
612 | 90 double d, d2=NAN; |
91 char *next= p->s; | |
92 int i; | |
93 | |
94 /* number */ | |
95 d= strtod(p->s, &next); | |
96 if(next != p->s){ | |
3729 | 97 if(*next >= 'E' && *next <= 'z'){ |
98 int e= si_prefixes[*next - 'E']; | |
99 if(e){ | |
100 if(next[1] == 'i'){ | |
101 d*= pow( 2, e/0.3); | |
102 next+=2; | |
103 }else{ | |
104 d*= pow(10, e); | |
105 next++; | |
106 } | |
107 } | |
108 } | |
612 | 109 p->s= next; |
2434 | 110 return d; |
612 | 111 } |
2967 | 112 |
612 | 113 /* named constants */ |
2433 | 114 for(i=0; p->const_name && p->const_name[i]; i++){ |
612 | 115 if(strmatch(p->s, p->const_name[i])){ |
116 p->s+= strlen(p->const_name[i]); | |
2434 | 117 return p->const_value[i]; |
612 | 118 } |
119 } | |
2967 | 120 |
612 | 121 p->s= strchr(p->s, '('); |
122 if(p->s==NULL){ | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1106
diff
changeset
|
123 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next); |
2434 | 124 return NAN; |
612 | 125 } |
126 p->s++; // "(" | |
2434 | 127 d= evalExpression(p); |
1815 | 128 if(p->s[0]== ','){ |
129 p->s++; // "," | |
2434 | 130 d2= evalExpression(p); |
612 | 131 } |
1815 | 132 if(p->s[0] != ')'){ |
133 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next); | |
2434 | 134 return NAN; |
1815 | 135 } |
136 p->s++; // ")" | |
2967 | 137 |
612 | 138 if( strmatch(next, "sinh" ) ) d= sinh(d); |
139 else if( strmatch(next, "cosh" ) ) d= cosh(d); | |
140 else if( strmatch(next, "tanh" ) ) d= tanh(d); | |
141 else if( strmatch(next, "sin" ) ) d= sin(d); | |
142 else if( strmatch(next, "cos" ) ) d= cos(d); | |
143 else if( strmatch(next, "tan" ) ) d= tan(d); | |
144 else if( strmatch(next, "exp" ) ) d= exp(d); | |
145 else if( strmatch(next, "log" ) ) d= log(d); | |
146 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d)); | |
147 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI); | |
1057 | 148 else if( strmatch(next, "abs" ) ) d= fabs(d); |
612 | 149 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2; |
150 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2; | |
151 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; | |
1815 | 152 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0; |
612 | 153 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; |
1815 | 154 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; |
612 | 155 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; |
2434 | 156 else if( strmatch(next, "(" ) ) d= d; |
612 | 157 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); |
158 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; | |
159 else{ | |
160 for(i=0; p->func1_name && p->func1_name[i]; i++){ | |
161 if(strmatch(next, p->func1_name[i])){ | |
2434 | 162 return p->func1[i](p->opaque, d); |
612 | 163 } |
164 } | |
165 | |
166 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
167 if(strmatch(next, p->func2_name[i])){ | |
2434 | 168 return p->func2[i](p->opaque, d, d2); |
612 | 169 } |
170 } | |
171 | |
2433 | 172 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next); |
2434 | 173 return NAN; |
612 | 174 } |
2433 | 175 |
2434 | 176 return d; |
2967 | 177 } |
2436 | 178 |
2434 | 179 static double evalPow(Parser *p){ |
2436 | 180 int sign= (*p->s == '+') - (*p->s == '-'); |
181 p->s += sign&1; | |
182 return (sign|1) * evalPrimary(p); | |
2434 | 183 } |
612 | 184 |
2434 | 185 static double evalFactor(Parser *p){ |
186 double ret= evalPow(p); | |
187 while(p->s[0]=='^'){ | |
612 | 188 p->s++; |
2434 | 189 ret= pow(ret, evalPow(p)); |
612 | 190 } |
2434 | 191 return ret; |
612 | 192 } |
193 | |
2434 | 194 static double evalTerm(Parser *p){ |
195 double ret= evalFactor(p); | |
196 while(p->s[0]=='*' || p->s[0]=='/'){ | |
197 if(*p->s++ == '*') ret*= evalFactor(p); | |
198 else ret/= evalFactor(p); | |
612 | 199 } |
2434 | 200 return ret; |
612 | 201 } |
202 | |
2434 | 203 static double evalExpression(Parser *p){ |
2436 | 204 double ret= 0; |
612 | 205 |
2434 | 206 if(p->stack_index <= 0) //protect against stack overflows |
207 return NAN; | |
208 p->stack_index--; | |
209 | |
2436 | 210 do{ |
211 ret += evalTerm(p); | |
212 }while(*p->s == '+' || *p->s == '-'); | |
612 | 213 |
2434 | 214 p->stack_index++; |
612 | 215 |
2434 | 216 return ret; |
612 | 217 } |
218 | |
1057 | 219 double ff_eval(char *s, double *const_value, const char **const_name, |
220 double (**func1)(void *, double), const char **func1_name, | |
612 | 221 double (**func2)(void *, double, double), char **func2_name, |
222 void *opaque){ | |
223 Parser p; | |
2967 | 224 |
2434 | 225 p.stack_index=100; |
612 | 226 p.s= s; |
227 p.const_value= const_value; | |
228 p.const_name = const_name; | |
229 p.func1 = func1; | |
230 p.func1_name = func1_name; | |
231 p.func2 = func2; | |
232 p.func2_name = func2_name; | |
233 p.opaque = opaque; | |
2967 | 234 |
2434 | 235 return evalExpression(&p); |
612 | 236 } |
2433 | 237 |
238 #ifdef TEST | |
2967 | 239 #undef printf |
2433 | 240 static double const_values[]={ |
241 M_PI, | |
242 M_E, | |
243 0 | |
244 }; | |
245 static const char *const_names[]={ | |
246 "PI", | |
247 "E", | |
248 0 | |
249 }; | |
250 main(){ | |
2436 | 251 int i; |
2433 | 252 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)); |
2967 | 253 |
2436 | 254 for(i=0; i<1050; i++){ |
255 START_TIMER | |
256 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); | |
257 STOP_TIMER("ff_eval") | |
258 } | |
2433 | 259 } |
260 #endif |