Mercurial > libavcodec.hg
annotate eval.c @ 3806:e1986d9ddc2d libavcodec
Fix compilation with --disable-encoders.
patch by Alexander Strange, astrange at ithinksw dot com
author | diego |
---|---|
date | Sun, 01 Oct 2006 18:19:49 +0000 |
parents | 3f7aa9fa5c98 |
children | 3a151ccc6ed7 |
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 |
3753 | 38 #define NAN 0.0/0.0 |
614
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; | |
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 | |
3778 | 58 static double evalExpression(Parser *p); |
59 | |
60 static int8_t si_prefixes['z' - 'E' + 1]={ | |
61 ['y'-'E']= -24, | |
62 ['z'-'E']= -21, | |
63 ['a'-'E']= -18, | |
64 ['f'-'E']= -15, | |
65 ['p'-'E']= -12, | |
66 ['n'-'E']= - 9, | |
67 ['u'-'E']= - 6, | |
68 ['m'-'E']= - 3, | |
69 ['c'-'E']= - 2, | |
70 ['d'-'E']= - 1, | |
71 ['h'-'E']= 2, | |
72 ['k'-'E']= 3, | |
73 ['K'-'E']= 3, | |
74 ['M'-'E']= 6, | |
75 ['G'-'E']= 9, | |
76 ['T'-'E']= 12, | |
77 ['P'-'E']= 15, | |
78 ['E'-'E']= 18, | |
79 ['Z'-'E']= 21, | |
80 ['Y'-'E']= 24, | |
81 }; | |
3756 | 82 |
3778 | 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. | |
86 */ | |
87 static double av_strtod(const char *name, char **tail) { | |
88 double d; | |
89 int p = 0; | |
90 char *next; | |
91 d = strtod(name, &next); | |
92 /* if parsing succeeded, check for and interpret postfixes */ | |
93 if (next!=name) { | |
94 | |
95 if(*next >= 'E' && *next <= 'z'){ | |
96 int e= si_prefixes[*next - 'E']; | |
97 if(e){ | |
98 if(next[1] == 'i'){ | |
99 d*= pow( 2, e/0.3); | |
100 next+=2; | |
101 }else{ | |
102 d*= pow(10, e); | |
103 next++; | |
104 } | |
105 } | |
106 } | |
107 | |
108 if(*next=='B') { | |
109 d*=8; | |
110 *next++; | |
111 } | |
112 } | |
113 /* if requested, fill in tail with the position after the last parsed | |
114 character */ | |
115 if (tail) | |
116 *tail = next; | |
117 return d; | |
118 } | |
612 | 119 |
1057 | 120 static int strmatch(const char *s, const char *prefix){ |
612 | 121 int i; |
122 for(i=0; prefix[i]; i++){ | |
123 if(prefix[i] != s[i]) return 0; | |
124 } | |
125 return 1; | |
126 } | |
127 | |
2434 | 128 static double evalPrimary(Parser *p){ |
612 | 129 double d, d2=NAN; |
130 char *next= p->s; | |
131 int i; | |
132 | |
133 /* number */ | |
3731
8b8773577dd9
Add support for SI (k, M, ...) and IEC/IEEE (Ki, Mi, ...) units.
takis
parents:
3730
diff
changeset
|
134 d= av_strtod(p->s, &next); |
612 | 135 if(next != p->s){ |
136 p->s= next; | |
2434 | 137 return d; |
612 | 138 } |
2967 | 139 |
612 | 140 /* named constants */ |
2433 | 141 for(i=0; p->const_name && p->const_name[i]; i++){ |
612 | 142 if(strmatch(p->s, p->const_name[i])){ |
143 p->s+= strlen(p->const_name[i]); | |
2434 | 144 return p->const_value[i]; |
612 | 145 } |
146 } | |
2967 | 147 |
612 | 148 p->s= strchr(p->s, '('); |
149 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
|
150 *p->error = "missing ("; |
3754 | 151 p->s= next; |
2434 | 152 return NAN; |
612 | 153 } |
154 p->s++; // "(" | |
2434 | 155 d= evalExpression(p); |
1815 | 156 if(p->s[0]== ','){ |
157 p->s++; // "," | |
2434 | 158 d2= evalExpression(p); |
612 | 159 } |
1815 | 160 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
|
161 *p->error = "missing )"; |
2434 | 162 return NAN; |
1815 | 163 } |
164 p->s++; // ")" | |
2967 | 165 |
612 | 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); | |
1057 | 176 else if( strmatch(next, "abs" ) ) d= fabs(d); |
3755 | 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; | |
612 | 183 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; |
2434 | 184 else if( strmatch(next, "(" ) ) d= d; |
612 | 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; | |
187 else{ | |
188 for(i=0; p->func1_name && p->func1_name[i]; i++){ | |
189 if(strmatch(next, p->func1_name[i])){ | |
2434 | 190 return p->func1[i](p->opaque, d); |
612 | 191 } |
192 } | |
193 | |
194 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
195 if(strmatch(next, p->func2_name[i])){ | |
2434 | 196 return p->func2[i](p->opaque, d, d2); |
612 | 197 } |
198 } | |
199 | |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
200 *p->error = "unknown function"; |
2434 | 201 return NAN; |
612 | 202 } |
2433 | 203 |
2434 | 204 return d; |
2967 | 205 } |
2436 | 206 |
2434 | 207 static double evalPow(Parser *p){ |
2436 | 208 int sign= (*p->s == '+') - (*p->s == '-'); |
209 p->s += sign&1; | |
210 return (sign|1) * evalPrimary(p); | |
2434 | 211 } |
612 | 212 |
2434 | 213 static double evalFactor(Parser *p){ |
214 double ret= evalPow(p); | |
215 while(p->s[0]=='^'){ | |
612 | 216 p->s++; |
2434 | 217 ret= pow(ret, evalPow(p)); |
612 | 218 } |
2434 | 219 return ret; |
612 | 220 } |
221 | |
2434 | 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); | |
612 | 227 } |
2434 | 228 return ret; |
612 | 229 } |
230 | |
2434 | 231 static double evalExpression(Parser *p){ |
2436 | 232 double ret= 0; |
612 | 233 |
2434 | 234 if(p->stack_index <= 0) //protect against stack overflows |
235 return NAN; | |
236 p->stack_index--; | |
237 | |
2436 | 238 do{ |
239 ret += evalTerm(p); | |
240 }while(*p->s == '+' || *p->s == '-'); | |
612 | 241 |
2434 | 242 p->stack_index++; |
612 | 243 |
2434 | 244 return ret; |
612 | 245 } |
246 | |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
247 double ff_eval2(char *s, double *const_value, const char **const_name, |
1057 | 248 double (**func1)(void *, double), const char **func1_name, |
612 | 249 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
|
250 void *opaque, char **error){ |
612 | 251 Parser p; |
2967 | 252 |
2434 | 253 p.stack_index=100; |
612 | 254 p.s= s; |
255 p.const_value= const_value; | |
256 p.const_name = const_name; | |
257 p.func1 = func1; | |
258 p.func1_name = func1_name; | |
259 p.func2 = func2; | |
260 p.func2_name = func2_name; | |
261 p.opaque = opaque; | |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
262 p.error= error; |
2967 | 263 |
2434 | 264 return evalExpression(&p); |
612 | 265 } |
2433 | 266 |
3779
3f7aa9fa5c98
Break compatibility only when first part of version number changes, in this
takis
parents:
3778
diff
changeset
|
267 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
268 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
|
269 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
|
270 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
|
271 void *opaque){ |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
272 char *error=NULL; |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
273 double ret; |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
274 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
|
275 if (error) |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
276 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
|
277 return ret; |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
278 } |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
279 #endif |
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
280 |
2433 | 281 #ifdef TEST |
2967 | 282 #undef printf |
2433 | 283 static double const_values[]={ |
284 M_PI, | |
285 M_E, | |
286 0 | |
287 }; | |
288 static const char *const_names[]={ | |
289 "PI", | |
290 "E", | |
291 0 | |
292 }; | |
293 main(){ | |
2436 | 294 int i; |
2433 | 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)); |
3730 | 296 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL)); |
2967 | 297 |
2436 | 298 for(i=0; i<1050; i++){ |
299 START_TIMER | |
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") | |
302 } | |
2433 | 303 } |
304 #endif |