|
808
|
1 /*
|
|
|
2 * simple arithmetic expression evaluator
|
|
|
3 *
|
|
|
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
|
|
|
5 *
|
|
|
6 * This file is part of FFmpeg.
|
|
|
7 *
|
|
|
8 * FFmpeg is free software; you can redistribute it and/or
|
|
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
|
10 * License as published by the Free Software Foundation; either
|
|
|
11 * version 2.1 of the License, or (at your option) any later version.
|
|
|
12 *
|
|
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
16 * Lesser General Public License for more details.
|
|
|
17 *
|
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
|
19 * License along with FFmpeg; if not, write to the Free Software
|
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
21 *
|
|
|
22 */
|
|
|
23
|
|
|
24 /**
|
|
|
25 * @file eval.c
|
|
|
26 * simple arithmetic expression evaluator.
|
|
|
27 *
|
|
|
28 * see http://joe.hotchkiss.com/programming/eval/eval.html
|
|
|
29 */
|
|
|
30
|
|
|
31 #include "avcodec.h"
|
|
|
32
|
|
|
33 #include <stdio.h>
|
|
|
34 #include <stdlib.h>
|
|
|
35 #include <string.h>
|
|
|
36 #include <math.h>
|
|
|
37
|
|
|
38 #ifndef NAN
|
|
|
39 #define NAN 0.0/0.0
|
|
|
40 #endif
|
|
|
41
|
|
|
42 #ifndef M_PI
|
|
|
43 #define M_PI 3.14159265358979323846
|
|
|
44 #endif
|
|
|
45
|
|
|
46 typedef struct Parser{
|
|
|
47 int stack_index;
|
|
|
48 char *s;
|
|
|
49 double *const_value;
|
|
|
50 const char **const_name; // NULL terminated
|
|
|
51 double (**func1)(void *, double a); // NULL terminated
|
|
|
52 const char **func1_name; // NULL terminated
|
|
|
53 double (**func2)(void *, double a, double b); // NULL terminated
|
|
|
54 char **func2_name; // NULL terminated
|
|
|
55 void *opaque;
|
|
|
56 char **error;
|
|
|
57 } Parser;
|
|
|
58
|
|
|
59 static double evalExpression(Parser *p);
|
|
|
60
|
|
|
61 static int8_t si_prefixes['z' - 'E' + 1]={
|
|
|
62 ['y'-'E']= -24,
|
|
|
63 ['z'-'E']= -21,
|
|
|
64 ['a'-'E']= -18,
|
|
|
65 ['f'-'E']= -15,
|
|
|
66 ['p'-'E']= -12,
|
|
|
67 ['n'-'E']= - 9,
|
|
|
68 ['u'-'E']= - 6,
|
|
|
69 ['m'-'E']= - 3,
|
|
|
70 ['c'-'E']= - 2,
|
|
|
71 ['d'-'E']= - 1,
|
|
|
72 ['h'-'E']= 2,
|
|
|
73 ['k'-'E']= 3,
|
|
|
74 ['K'-'E']= 3,
|
|
|
75 ['M'-'E']= 6,
|
|
|
76 ['G'-'E']= 9,
|
|
|
77 ['T'-'E']= 12,
|
|
|
78 ['P'-'E']= 15,
|
|
|
79 ['E'-'E']= 18,
|
|
|
80 ['Z'-'E']= 21,
|
|
|
81 ['Y'-'E']= 24,
|
|
|
82 };
|
|
|
83
|
|
|
84 /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
|
|
|
85 * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
|
|
|
86 * function assumes that the unit of numbers is bits not bytes.
|
|
|
87 */
|
|
|
88 static double av_strtod(const char *name, char **tail) {
|
|
|
89 double d;
|
|
|
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 }
|
|
|
119
|
|
|
120 static int strmatch(const char *s, const char *prefix){
|
|
|
121 int i;
|
|
|
122 for(i=0; prefix[i]; i++){
|
|
|
123 if(prefix[i] != s[i]) return 0;
|
|
|
124 }
|
|
|
125 return 1;
|
|
|
126 }
|
|
|
127
|
|
|
128 static double evalPrimary(Parser *p){
|
|
|
129 double d, d2=NAN;
|
|
|
130 char *next= p->s;
|
|
|
131 int i;
|
|
|
132
|
|
|
133 /* number */
|
|
|
134 d= av_strtod(p->s, &next);
|
|
|
135 if(next != p->s){
|
|
|
136 p->s= next;
|
|
|
137 return d;
|
|
|
138 }
|
|
|
139
|
|
|
140 /* named constants */
|
|
|
141 for(i=0; p->const_name && p->const_name[i]; i++){
|
|
|
142 if(strmatch(p->s, p->const_name[i])){
|
|
|
143 p->s+= strlen(p->const_name[i]);
|
|
|
144 return p->const_value[i];
|
|
|
145 }
|
|
|
146 }
|
|
|
147
|
|
|
148 p->s= strchr(p->s, '(');
|
|
|
149 if(p->s==NULL){
|
|
|
150 *p->error = "missing (";
|
|
|
151 p->s= next;
|
|
|
152 return NAN;
|
|
|
153 }
|
|
|
154 p->s++; // "("
|
|
|
155 d= evalExpression(p);
|
|
|
156 if(p->s[0]== ','){
|
|
|
157 p->s++; // ","
|
|
|
158 d2= evalExpression(p);
|
|
|
159 }
|
|
|
160 if(p->s[0] != ')'){
|
|
|
161 *p->error = "missing )";
|
|
|
162 return NAN;
|
|
|
163 }
|
|
|
164 p->s++; // ")"
|
|
|
165
|
|
|
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, "atan" ) ) d= atan(d);
|
|
|
173 else if( strmatch(next, "asin" ) ) d= asin(d);
|
|
|
174 else if( strmatch(next, "acos" ) ) d= acos(d);
|
|
|
175 else if( strmatch(next, "exp" ) ) d= exp(d);
|
|
|
176 else if( strmatch(next, "log" ) ) d= log(d);
|
|
|
177 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
|
|
|
178 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
|
|
|
179 else if( strmatch(next, "abs" ) ) d= fabs(d);
|
|
|
180 else if( strmatch(next, "mod" ) ) d-= floor(d/d2)*d2;
|
|
|
181 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
|
|
|
182 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
|
|
|
183 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
|
|
|
184 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
|
|
|
185 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
|
|
|
186 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
|
|
|
187 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
|
|
|
188 else if( strmatch(next, "(" ) ) d= d;
|
|
|
189 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
|
|
|
190 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
|
|
|
191 else{
|
|
|
192 for(i=0; p->func1_name && p->func1_name[i]; i++){
|
|
|
193 if(strmatch(next, p->func1_name[i])){
|
|
|
194 return p->func1[i](p->opaque, d);
|
|
|
195 }
|
|
|
196 }
|
|
|
197
|
|
|
198 for(i=0; p->func2_name && p->func2_name[i]; i++){
|
|
|
199 if(strmatch(next, p->func2_name[i])){
|
|
|
200 return p->func2[i](p->opaque, d, d2);
|
|
|
201 }
|
|
|
202 }
|
|
|
203
|
|
|
204 *p->error = "unknown function";
|
|
|
205 return NAN;
|
|
|
206 }
|
|
|
207
|
|
|
208 return d;
|
|
|
209 }
|
|
|
210
|
|
|
211 static double evalPow(Parser *p, int *sign){
|
|
|
212 *sign= (*p->s == '+') - (*p->s == '-');
|
|
|
213 p->s += *sign&1;
|
|
|
214 return evalPrimary(p);
|
|
|
215 }
|
|
|
216
|
|
|
217 static double evalFactor(Parser *p){
|
|
|
218 int sign, sign2;
|
|
|
219 double ret, e;
|
|
|
220 ret= evalPow(p, &sign);
|
|
|
221 while(p->s[0]=='^'){
|
|
|
222 p->s++;
|
|
|
223 e= evalPow(p, &sign2);
|
|
|
224 ret= pow(ret, (sign2|1) * e);
|
|
|
225 }
|
|
|
226 return (sign|1) * ret;
|
|
|
227 }
|
|
|
228
|
|
|
229 static double evalTerm(Parser *p){
|
|
|
230 double ret= evalFactor(p);
|
|
|
231 while(p->s[0]=='*' || p->s[0]=='/'){
|
|
|
232 if(*p->s++ == '*') ret*= evalFactor(p);
|
|
|
233 else ret/= evalFactor(p);
|
|
|
234 }
|
|
|
235 return ret;
|
|
|
236 }
|
|
|
237
|
|
|
238 static double evalExpression(Parser *p){
|
|
|
239 double ret= 0;
|
|
|
240
|
|
|
241 if(p->stack_index <= 0) //protect against stack overflows
|
|
|
242 return NAN;
|
|
|
243 p->stack_index--;
|
|
|
244
|
|
|
245 do{
|
|
|
246 ret += evalTerm(p);
|
|
|
247 }while(*p->s == '+' || *p->s == '-');
|
|
|
248
|
|
|
249 p->stack_index++;
|
|
|
250
|
|
|
251 return ret;
|
|
|
252 }
|
|
|
253
|
|
|
254 double ff_eval2(char *s, double *const_value, const char **const_name,
|
|
|
255 double (**func1)(void *, double), const char **func1_name,
|
|
|
256 double (**func2)(void *, double, double), char **func2_name,
|
|
|
257 void *opaque, char **error){
|
|
|
258 Parser p;
|
|
|
259
|
|
|
260 p.stack_index=100;
|
|
|
261 p.s= s;
|
|
|
262 p.const_value= const_value;
|
|
|
263 p.const_name = const_name;
|
|
|
264 p.func1 = func1;
|
|
|
265 p.func1_name = func1_name;
|
|
|
266 p.func2 = func2;
|
|
|
267 p.func2_name = func2_name;
|
|
|
268 p.opaque = opaque;
|
|
|
269 p.error= error;
|
|
|
270
|
|
|
271 return evalExpression(&p);
|
|
|
272 }
|
|
|
273
|
|
|
274 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
|
|
|
275 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
|
|
|
276 double (**func1)(void *, double), const char **func1_name,
|
|
|
277 double (**func2)(void *, double, double), char **func2_name,
|
|
|
278 void *opaque){
|
|
|
279 char *error=NULL;
|
|
|
280 double ret;
|
|
|
281 ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
|
|
|
282 if (error)
|
|
|
283 av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
|
|
|
284 return ret;
|
|
|
285 }
|
|
|
286 #endif
|
|
|
287
|
|
|
288 #ifdef TEST
|
|
|
289 #undef printf
|
|
|
290 static double const_values[]={
|
|
|
291 M_PI,
|
|
|
292 M_E,
|
|
|
293 0
|
|
|
294 };
|
|
|
295 static const char *const_names[]={
|
|
|
296 "PI",
|
|
|
297 "E",
|
|
|
298 0
|
|
|
299 };
|
|
|
300 main(){
|
|
|
301 int i;
|
|
|
302 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));
|
|
|
303 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
|
|
|
304
|
|
|
305 for(i=0; i<1050; i++){
|
|
|
306 START_TIMER
|
|
|
307 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);
|
|
|
308 STOP_TIMER("ff_eval")
|
|
|
309 }
|
|
|
310 }
|
|
|
311 #endif
|