Mercurial > libavcodec.hg
comparison eval.c @ 2434:24aa9209e8b0 libavcodec
simplify
author | michael |
---|---|
date | Sat, 15 Jan 2005 20:04:41 +0000 |
parents | 0934621b6453 |
children | 86d14aebd527 |
comparison
equal
deleted
inserted
replaced
2433:0934621b6453 | 2434:24aa9209e8b0 |
---|---|
40 | 40 |
41 #ifndef M_PI | 41 #ifndef M_PI |
42 #define M_PI 3.14159265358979323846 | 42 #define M_PI 3.14159265358979323846 |
43 #endif | 43 #endif |
44 | 44 |
45 #define STACK_SIZE 100 | |
46 | |
47 typedef struct Parser{ | 45 typedef struct Parser{ |
48 double stack[STACK_SIZE]; | |
49 int stack_index; | 46 int stack_index; |
50 char *s; | 47 char *s; |
51 double *const_value; | 48 double *const_value; |
52 const char **const_name; // NULL terminated | 49 const char **const_name; // NULL terminated |
53 double (**func1)(void *, double a); // NULL terminated | 50 double (**func1)(void *, double a); // NULL terminated |
55 double (**func2)(void *, double a, double b); // NULL terminated | 52 double (**func2)(void *, double a, double b); // NULL terminated |
56 char **func2_name; // NULL terminated | 53 char **func2_name; // NULL terminated |
57 void *opaque; | 54 void *opaque; |
58 } Parser; | 55 } Parser; |
59 | 56 |
60 static void evalExpression(Parser *p); | 57 static double evalExpression(Parser *p); |
61 | |
62 static void push(Parser *p, double d){ | |
63 if(p->stack_index+1>= STACK_SIZE){ | |
64 av_log(NULL, AV_LOG_ERROR, "stack overflow in the parser\n"); | |
65 return; | |
66 } | |
67 p->stack[ p->stack_index++ ]= d; | |
68 //printf("push %f\n", d); fflush(stdout); | |
69 } | |
70 | |
71 static double pop(Parser *p){ | |
72 if(p->stack_index<=0){ | |
73 av_log(NULL, AV_LOG_ERROR, "stack underflow in the parser\n"); | |
74 return NAN; | |
75 } | |
76 //printf("pop\n"); fflush(stdout); | |
77 return p->stack[ --p->stack_index ]; | |
78 } | |
79 | 58 |
80 static int strmatch(const char *s, const char *prefix){ | 59 static int strmatch(const char *s, const char *prefix){ |
81 int i; | 60 int i; |
82 for(i=0; prefix[i]; i++){ | 61 for(i=0; prefix[i]; i++){ |
83 if(prefix[i] != s[i]) return 0; | 62 if(prefix[i] != s[i]) return 0; |
84 } | 63 } |
85 return 1; | 64 return 1; |
86 } | 65 } |
87 | 66 |
88 static void evalPrimary(Parser *p){ | 67 static double evalPrimary(Parser *p){ |
89 double d, d2=NAN; | 68 double d, d2=NAN; |
90 char *next= p->s; | 69 char *next= p->s; |
91 int i; | 70 int i; |
92 | 71 |
93 /* number */ | 72 /* number */ |
94 d= strtod(p->s, &next); | 73 d= strtod(p->s, &next); |
95 if(next != p->s){ | 74 if(next != p->s){ |
96 push(p, d); | |
97 p->s= next; | 75 p->s= next; |
98 return; | 76 return d; |
99 } | 77 } |
100 | 78 |
101 /* named constants */ | 79 /* named constants */ |
102 for(i=0; p->const_name && p->const_name[i]; i++){ | 80 for(i=0; p->const_name && p->const_name[i]; i++){ |
103 if(strmatch(p->s, p->const_name[i])){ | 81 if(strmatch(p->s, p->const_name[i])){ |
104 push(p, p->const_value[i]); | |
105 p->s+= strlen(p->const_name[i]); | 82 p->s+= strlen(p->const_name[i]); |
106 return; | 83 return p->const_value[i]; |
107 } | 84 } |
108 } | 85 } |
109 | 86 |
110 p->s= strchr(p->s, '('); | 87 p->s= strchr(p->s, '('); |
111 if(p->s==NULL){ | 88 if(p->s==NULL){ |
112 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next); | 89 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next); |
113 return; | 90 return NAN; |
114 } | 91 } |
115 p->s++; // "(" | 92 p->s++; // "(" |
116 evalExpression(p); | 93 d= evalExpression(p); |
117 d= pop(p); | |
118 if(p->s[0]== ','){ | 94 if(p->s[0]== ','){ |
119 p->s++; // "," | 95 p->s++; // "," |
120 evalExpression(p); | 96 d2= evalExpression(p); |
121 d2= pop(p); | |
122 } | 97 } |
123 if(p->s[0] != ')'){ | 98 if(p->s[0] != ')'){ |
124 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next); | 99 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next); |
125 return; | 100 return NAN; |
126 } | 101 } |
127 p->s++; // ")" | 102 p->s++; // ")" |
128 | 103 |
129 if( strmatch(next, "sinh" ) ) d= sinh(d); | 104 if( strmatch(next, "sinh" ) ) d= sinh(d); |
130 else if( strmatch(next, "cosh" ) ) d= cosh(d); | 105 else if( strmatch(next, "cosh" ) ) d= cosh(d); |
142 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; | 117 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; |
143 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0; | 118 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0; |
144 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; | 119 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; |
145 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; | 120 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; |
146 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; | 121 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; |
122 else if( strmatch(next, "(" ) ) d= d; | |
147 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); | 123 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); |
148 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; | 124 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; |
149 else{ | 125 else{ |
150 for(i=0; p->func1_name && p->func1_name[i]; i++){ | 126 for(i=0; p->func1_name && p->func1_name[i]; i++){ |
151 if(strmatch(next, p->func1_name[i])){ | 127 if(strmatch(next, p->func1_name[i])){ |
152 d= p->func1[i](p->opaque, d); | 128 return p->func1[i](p->opaque, d); |
153 goto push_ret; | |
154 } | 129 } |
155 } | 130 } |
156 | 131 |
157 for(i=0; p->func2_name && p->func2_name[i]; i++){ | 132 for(i=0; p->func2_name && p->func2_name[i]; i++){ |
158 if(strmatch(next, p->func2_name[i])){ | 133 if(strmatch(next, p->func2_name[i])){ |
159 d= p->func2[i](p->opaque, d, d2); | 134 return p->func2[i](p->opaque, d, d2); |
160 goto push_ret; | |
161 } | 135 } |
162 } | 136 } |
163 | 137 |
164 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next); | 138 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next); |
165 return; | 139 return NAN; |
166 } | 140 } |
167 | 141 |
168 push_ret: | 142 return d; |
169 push(p, d); | |
170 } | 143 } |
171 | 144 |
172 static void evalPow(Parser *p){ | 145 static double evalPow(Parser *p){ |
173 int neg= 0; | |
174 if(p->s[0]=='+') p->s++; | 146 if(p->s[0]=='+') p->s++; |
175 | 147 |
176 if(p->s[0]=='-'){ | 148 if(p->s[0]=='-'){ |
177 neg= 1; | |
178 p->s++; | 149 p->s++; |
179 } | 150 return -evalPrimary(p); |
180 | 151 }else |
181 if(p->s[0]=='('){ | 152 return evalPrimary(p); |
153 } | |
154 | |
155 static double evalFactor(Parser *p){ | |
156 double ret= evalPow(p); | |
157 while(p->s[0]=='^'){ | |
182 p->s++; | 158 p->s++; |
183 evalExpression(p); | 159 ret= pow(ret, evalPow(p)); |
184 | 160 } |
185 if(p->s[0]!=')') | 161 return ret; |
186 av_log(NULL, AV_LOG_ERROR, "Parser: missing )\n"); | 162 } |
187 p->s++; | 163 |
188 }else{ | 164 static double evalTerm(Parser *p){ |
189 evalPrimary(p); | 165 double ret= evalFactor(p); |
190 } | |
191 | |
192 if(neg) push(p, -pop(p)); | |
193 } | |
194 | |
195 static void evalFactor(Parser *p){ | |
196 evalPow(p); | |
197 while(p->s[0]=='^'){ | |
198 double d; | |
199 | |
200 p->s++; | |
201 evalPow(p); | |
202 d= pop(p); | |
203 push(p, pow(pop(p), d)); | |
204 } | |
205 } | |
206 | |
207 static void evalTerm(Parser *p){ | |
208 evalFactor(p); | |
209 while(p->s[0]=='*' || p->s[0]=='/'){ | 166 while(p->s[0]=='*' || p->s[0]=='/'){ |
210 int inv= p->s[0]=='/'; | 167 if(*p->s++ == '*') ret*= evalFactor(p); |
211 double d; | 168 else ret/= evalFactor(p); |
212 | 169 } |
213 p->s++; | 170 return ret; |
214 evalFactor(p); | 171 } |
215 d= pop(p); | 172 |
216 if(inv) d= 1.0/d; | 173 static double evalExpression(Parser *p){ |
217 push(p, d * pop(p)); | 174 double ret; |
218 } | 175 |
219 } | 176 if(p->stack_index <= 0) //protect against stack overflows |
220 | 177 return NAN; |
221 static void evalExpression(Parser *p){ | 178 p->stack_index--; |
222 evalTerm(p); | 179 |
180 ret= evalTerm(p); | |
223 while(p->s[0]=='+' || p->s[0]=='-'){ | 181 while(p->s[0]=='+' || p->s[0]=='-'){ |
224 int sign= p->s[0]=='-'; | 182 if(*p->s++ == '+') ret+= evalTerm(p); |
225 double d; | 183 else ret-= evalTerm(p); |
226 | 184 } |
227 p->s++; | 185 |
228 evalTerm(p); | 186 p->stack_index++; |
229 d= pop(p); | 187 |
230 if(sign) d= -d; | 188 return ret; |
231 push(p, d + pop(p)); | |
232 } | |
233 } | 189 } |
234 | 190 |
235 double ff_eval(char *s, double *const_value, const char **const_name, | 191 double ff_eval(char *s, double *const_value, const char **const_name, |
236 double (**func1)(void *, double), const char **func1_name, | 192 double (**func1)(void *, double), const char **func1_name, |
237 double (**func2)(void *, double, double), char **func2_name, | 193 double (**func2)(void *, double, double), char **func2_name, |
238 void *opaque){ | 194 void *opaque){ |
239 Parser p; | 195 Parser p; |
240 | 196 |
241 p.stack_index=0; | 197 p.stack_index=100; |
242 p.s= s; | 198 p.s= s; |
243 p.const_value= const_value; | 199 p.const_value= const_value; |
244 p.const_name = const_name; | 200 p.const_name = const_name; |
245 p.func1 = func1; | 201 p.func1 = func1; |
246 p.func1_name = func1_name; | 202 p.func1_name = func1_name; |
247 p.func2 = func2; | 203 p.func2 = func2; |
248 p.func2_name = func2_name; | 204 p.func2_name = func2_name; |
249 p.opaque = opaque; | 205 p.opaque = opaque; |
250 | 206 |
251 evalExpression(&p); | 207 return evalExpression(&p); |
252 return pop(&p); | |
253 } | 208 } |
254 | 209 |
255 #ifdef TEST | 210 #ifdef TEST |
256 #undef printf | 211 #undef printf |
257 static double const_values[]={ | 212 static double const_values[]={ |