Mercurial > libavcodec.hg
annotate eval.c @ 699:f69f9cb461bc libavcodec
automatically detect broken opendivx codec and workaround, so bug=1 shouldnt be needed anymore for mpeg4 files, if there still are files which need bug=1 then tell me ...
author | michaelni |
---|---|
date | Thu, 26 Sep 2002 22:37:33 +0000 |
parents | 79c43f519d02 |
children | bb5de8a59da8 |
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 | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 /* | |
23 * see http://joe.hotchkiss.com/programming/eval/eval.html | |
24 */ | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <math.h> | |
30 | |
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
31 #ifndef NAN |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
32 #define NAN 0 |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
33 #endif |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
34 |
627 | 35 #ifndef M_PI |
36 #define M_PI 3.14159265358979323846 | |
37 #endif | |
38 | |
612 | 39 #define STACK_SIZE 100 |
40 | |
41 typedef struct Parser{ | |
42 double stack[STACK_SIZE]; | |
43 int stack_index; | |
44 char *s; | |
45 double *const_value; | |
46 char **const_name; // NULL terminated | |
47 double (**func1)(void *, double a); // NULL terminated | |
48 char **func1_name; // NULL terminated | |
49 double (**func2)(void *, double a, double b); // NULL terminated | |
50 char **func2_name; // NULL terminated | |
51 void *opaque; | |
52 } Parser; | |
53 | |
54 static void evalExpression(Parser *p); | |
55 | |
56 static void push(Parser *p, double d){ | |
57 if(p->stack_index+1>= STACK_SIZE){ | |
58 fprintf(stderr, "stack overflow in the parser\n"); | |
59 return; | |
60 } | |
61 p->stack[ p->stack_index++ ]= d; | |
62 //printf("push %f\n", d); fflush(stdout); | |
63 } | |
64 | |
65 static double pop(Parser *p){ | |
66 if(p->stack_index<=0){ | |
67 fprintf(stderr, "stack underflow in the parser\n"); | |
68 return NAN; | |
69 } | |
70 //printf("pop\n"); fflush(stdout); | |
71 return p->stack[ --p->stack_index ]; | |
72 } | |
73 | |
74 static int strmatch(char *s, char *prefix){ | |
75 int i; | |
76 for(i=0; prefix[i]; i++){ | |
77 if(prefix[i] != s[i]) return 0; | |
78 } | |
79 return 1; | |
80 } | |
81 | |
82 static void evalPrimary(Parser *p){ | |
83 double d, d2=NAN; | |
84 char *next= p->s; | |
85 int i; | |
86 | |
87 /* number */ | |
88 d= strtod(p->s, &next); | |
89 if(next != p->s){ | |
90 push(p, d); | |
91 p->s= next; | |
92 return; | |
93 } | |
94 | |
95 /* named constants */ | |
96 for(i=0; p->const_name[i]; i++){ | |
97 if(strmatch(p->s, p->const_name[i])){ | |
98 push(p, p->const_value[i]); | |
99 p->s+= strlen(p->const_name[i]); | |
100 return; | |
101 } | |
102 } | |
103 | |
104 p->s= strchr(p->s, '('); | |
105 if(p->s==NULL){ | |
106 fprintf(stderr, "Parser: missing ( in \"%s\"\n", next); | |
107 return; | |
108 } | |
109 p->s++; // "(" | |
110 evalExpression(p); | |
111 d= pop(p); | |
112 p->s++; // ")" or "," | |
113 if(p->s[-1]== ','){ | |
114 evalExpression(p); | |
115 d2= pop(p); | |
116 p->s++; // ")" | |
117 } | |
118 | |
119 if( strmatch(next, "sinh" ) ) d= sinh(d); | |
120 else if( strmatch(next, "cosh" ) ) d= cosh(d); | |
121 else if( strmatch(next, "tanh" ) ) d= tanh(d); | |
122 else if( strmatch(next, "sin" ) ) d= sin(d); | |
123 else if( strmatch(next, "cos" ) ) d= cos(d); | |
124 else if( strmatch(next, "tan" ) ) d= tan(d); | |
125 else if( strmatch(next, "exp" ) ) d= exp(d); | |
126 else if( strmatch(next, "log" ) ) d= log(d); | |
127 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d)); | |
128 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI); | |
129 else if( strmatch(next, "abs" ) ) d= abs(d); | |
130 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2; | |
131 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2; | |
132 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; | |
133 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; | |
134 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; | |
135 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); | |
136 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; | |
137 else{ | |
138 int error=1; | |
139 for(i=0; p->func1_name && p->func1_name[i]; i++){ | |
140 if(strmatch(next, p->func1_name[i])){ | |
141 d= p->func1[i](p->opaque, d); | |
142 error=0; | |
143 break; | |
144 } | |
145 } | |
146 | |
147 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
148 if(strmatch(next, p->func2_name[i])){ | |
149 d= p->func2[i](p->opaque, d, d2); | |
150 error=0; | |
151 break; | |
152 } | |
153 } | |
154 | |
155 if(error){ | |
156 fprintf(stderr, "Parser: unknown function in \"%s\"\n", next); | |
157 return; | |
158 } | |
159 } | |
160 | |
161 if(p->s[-1]!= ')'){ | |
162 fprintf(stderr, "Parser: missing ) in \"%s\"\n", next); | |
163 return; | |
164 } | |
165 push(p, d); | |
166 } | |
167 | |
168 static void evalPow(Parser *p){ | |
169 int neg= 0; | |
170 if(p->s[0]=='+') p->s++; | |
171 | |
172 if(p->s[0]=='-'){ | |
173 neg= 1; | |
174 p->s++; | |
175 } | |
176 | |
177 if(p->s[0]=='('){ | |
178 p->s++;; | |
179 evalExpression(p); | |
180 | |
181 if(p->s[0]!=')') | |
182 fprintf(stderr, "Parser: missing )\n"); | |
183 p->s++; | |
184 }else{ | |
185 evalPrimary(p); | |
186 } | |
187 | |
188 if(neg) push(p, -pop(p)); | |
189 } | |
190 | |
191 static void evalFactor(Parser *p){ | |
192 evalPow(p); | |
193 while(p->s[0]=='^'){ | |
194 double d; | |
195 | |
196 p->s++; | |
197 evalPow(p); | |
198 d= pop(p); | |
199 push(p, pow(pop(p), d)); | |
200 } | |
201 } | |
202 | |
203 static void evalTerm(Parser *p){ | |
204 evalFactor(p); | |
205 while(p->s[0]=='*' || p->s[0]=='/'){ | |
206 int inv= p->s[0]=='/'; | |
207 double d; | |
208 | |
209 p->s++; | |
210 evalFactor(p); | |
211 d= pop(p); | |
212 if(inv) d= 1.0/d; | |
213 push(p, d * pop(p)); | |
214 } | |
215 } | |
216 | |
217 static void evalExpression(Parser *p){ | |
218 evalTerm(p); | |
219 while(p->s[0]=='+' || p->s[0]=='-'){ | |
220 int sign= p->s[0]=='-'; | |
221 double d; | |
222 | |
223 p->s++; | |
224 evalTerm(p); | |
225 d= pop(p); | |
226 if(sign) d= -d; | |
227 push(p, d + pop(p)); | |
228 } | |
229 } | |
230 | |
231 double ff_eval(char *s, double *const_value, char **const_name, | |
232 double (**func1)(void *, double), char **func1_name, | |
233 double (**func2)(void *, double, double), char **func2_name, | |
234 void *opaque){ | |
235 Parser p; | |
236 | |
237 p.stack_index=0; | |
238 p.s= s; | |
239 p.const_value= const_value; | |
240 p.const_name = const_name; | |
241 p.func1 = func1; | |
242 p.func1_name = func1_name; | |
243 p.func2 = func2; | |
244 p.func2_name = func2_name; | |
245 p.opaque = opaque; | |
246 | |
247 evalExpression(&p); | |
248 return pop(&p); | |
249 } |