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 typedef struct Parser{
|
|
39 int stack_index;
|
|
40 char *s;
|
|
41 double *const_value;
|
|
42 const char **const_name; // NULL terminated
|
|
43 double (**func1)(void *, double a); // NULL terminated
|
|
44 const char **func1_name; // NULL terminated
|
|
45 double (**func2)(void *, double a, double b); // NULL terminated
|
|
46 char **func2_name; // NULL terminated
|
|
47 void *opaque;
|
|
48 char **error;
|
|
49 } Parser;
|
|
50
|
|
51 static double evalExpression(Parser *p);
|
|
52
|
|
53 static int8_t si_prefixes['z' - 'E' + 1]={
|
|
54 ['y'-'E']= -24,
|
|
55 ['z'-'E']= -21,
|
|
56 ['a'-'E']= -18,
|
|
57 ['f'-'E']= -15,
|
|
58 ['p'-'E']= -12,
|
|
59 ['n'-'E']= - 9,
|
|
60 ['u'-'E']= - 6,
|
|
61 ['m'-'E']= - 3,
|
|
62 ['c'-'E']= - 2,
|
|
63 ['d'-'E']= - 1,
|
|
64 ['h'-'E']= 2,
|
|
65 ['k'-'E']= 3,
|
|
66 ['K'-'E']= 3,
|
|
67 ['M'-'E']= 6,
|
|
68 ['G'-'E']= 9,
|
|
69 ['T'-'E']= 12,
|
|
70 ['P'-'E']= 15,
|
|
71 ['E'-'E']= 18,
|
|
72 ['Z'-'E']= 21,
|
|
73 ['Y'-'E']= 24,
|
|
74 };
|
|
75
|
|
76 /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
|
|
77 * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
|
|
78 * function assumes that the unit of numbers is bits not bytes.
|
|
79 */
|
|
80 static double av_strtod(const char *name, char **tail) {
|
|
81 double d;
|
|
82 char *next;
|
|
83 d = strtod(name, &next);
|
|
84 /* if parsing succeeded, check for and interpret postfixes */
|
|
85 if (next!=name) {
|
|
86
|
|
87 if(*next >= 'E' && *next <= 'z'){
|
|
88 int e= si_prefixes[*next - 'E'];
|
|
89 if(e){
|
|
90 if(next[1] == 'i'){
|
|
91 d*= pow( 2, e/0.3);
|
|
92 next+=2;
|
|
93 }else{
|
|
94 d*= pow(10, e);
|
|
95 next++;
|
|
96 }
|
|
97 }
|
|
98 }
|
|
99
|
|
100 if(*next=='B') {
|
|
101 d*=8;
|
|
102 *next++;
|
|
103 }
|
|
104 }
|
|
105 /* if requested, fill in tail with the position after the last parsed
|
|
106 character */
|
|
107 if (tail)
|
|
108 *tail = next;
|
|
109 return d;
|
|
110 }
|
|
111
|
|
112 static int strmatch(const char *s, const char *prefix){
|
|
113 int i;
|
|
114 for(i=0; prefix[i]; i++){
|
|
115 if(prefix[i] != s[i]) return 0;
|
|
116 }
|
|
117 return 1;
|
|
118 }
|
|
119
|
|
120 static double evalPrimary(Parser *p){
|
|
121 double d, d2=NAN;
|
|
122 char *next= p->s;
|
|
123 int i;
|
|
124
|
|
125 /* number */
|
|
126 d= av_strtod(p->s, &next);
|
|
127 if(next != p->s){
|
|
128 p->s= next;
|
|
129 return d;
|
|
130 }
|
|
131
|
|
132 /* named constants */
|
|
133 for(i=0; p->const_name && p->const_name[i]; i++){
|
|
134 if(strmatch(p->s, p->const_name[i])){
|
|
135 p->s+= strlen(p->const_name[i]);
|
|
136 return p->const_value[i];
|
|
137 }
|
|
138 }
|
|
139
|
|
140 p->s= strchr(p->s, '(');
|
|
141 if(p->s==NULL){
|
|
142 *p->error = "missing (";
|
|
143 p->s= next;
|
|
144 return NAN;
|
|
145 }
|
|
146 p->s++; // "("
|
|
147 d= evalExpression(p);
|
|
148 if(p->s[0]== ','){
|
|
149 p->s++; // ","
|
|
150 d2= evalExpression(p);
|
|
151 }
|
|
152 if(p->s[0] != ')'){
|
|
153 *p->error = "missing )";
|
|
154 return NAN;
|
|
155 }
|
|
156 p->s++; // ")"
|
|
157
|
|
158 if( strmatch(next, "sinh" ) ) d= sinh(d);
|
|
159 else if( strmatch(next, "cosh" ) ) d= cosh(d);
|
|
160 else if( strmatch(next, "tanh" ) ) d= tanh(d);
|
|
161 else if( strmatch(next, "sin" ) ) d= sin(d);
|
|
162 else if( strmatch(next, "cos" ) ) d= cos(d);
|
|
163 else if( strmatch(next, "tan" ) ) d= tan(d);
|
|
164 else if( strmatch(next, "atan" ) ) d= atan(d);
|
|
165 else if( strmatch(next, "asin" ) ) d= asin(d);
|
|
166 else if( strmatch(next, "acos" ) ) d= acos(d);
|
|
167 else if( strmatch(next, "exp" ) ) d= exp(d);
|
|
168 else if( strmatch(next, "log" ) ) d= log(d);
|
|
169 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
|
|
170 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
|
|
171 else if( strmatch(next, "abs" ) ) d= fabs(d);
|
|
172 else if( strmatch(next, "mod" ) ) d-= floor(d/d2)*d2;
|
|
173 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
|
|
174 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
|
|
175 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
|
|
176 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
|
|
177 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
|
|
178 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
|
|
179 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
|
|
180 else if( strmatch(next, "(" ) ) d= d;
|
|
181 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
|
|
182 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
|
|
183 else{
|
|
184 for(i=0; p->func1_name && p->func1_name[i]; i++){
|
|
185 if(strmatch(next, p->func1_name[i])){
|
|
186 return p->func1[i](p->opaque, d);
|
|
187 }
|
|
188 }
|
|
189
|
|
190 for(i=0; p->func2_name && p->func2_name[i]; i++){
|
|
191 if(strmatch(next, p->func2_name[i])){
|
|
192 return p->func2[i](p->opaque, d, d2);
|
|
193 }
|
|
194 }
|
|
195
|
|
196 *p->error = "unknown function";
|
|
197 return NAN;
|
|
198 }
|
|
199
|
|
200 return d;
|
|
201 }
|
|
202
|
|
203 static double evalPow(Parser *p, int *sign){
|
|
204 *sign= (*p->s == '+') - (*p->s == '-');
|
|
205 p->s += *sign&1;
|
|
206 return evalPrimary(p);
|
|
207 }
|
|
208
|
|
209 static double evalFactor(Parser *p){
|
|
210 int sign, sign2;
|
|
211 double ret, e;
|
|
212 ret= evalPow(p, &sign);
|
|
213 while(p->s[0]=='^'){
|
|
214 p->s++;
|
|
215 e= evalPow(p, &sign2);
|
|
216 ret= pow(ret, (sign2|1) * e);
|
|
217 }
|
|
218 return (sign|1) * ret;
|
|
219 }
|
|
220
|
|
221 static double evalTerm(Parser *p){
|
|
222 double ret= evalFactor(p);
|
|
223 while(p->s[0]=='*' || p->s[0]=='/'){
|
|
224 if(*p->s++ == '*') ret*= evalFactor(p);
|
|
225 else ret/= evalFactor(p);
|
|
226 }
|
|
227 return ret;
|
|
228 }
|
|
229
|
|
230 static double evalExpression(Parser *p){
|
|
231 double ret= 0;
|
|
232
|
|
233 if(p->stack_index <= 0) //protect against stack overflows
|
|
234 return NAN;
|
|
235 p->stack_index--;
|
|
236
|
|
237 do{
|
|
238 ret += evalTerm(p);
|
|
239 }while(*p->s == '+' || *p->s == '-');
|
|
240
|
|
241 p->stack_index++;
|
|
242
|
|
243 return ret;
|
|
244 }
|
|
245
|
|
246 double ff_eval2(char *s, double *const_value, const char **const_name,
|
|
247 double (**func1)(void *, double), const char **func1_name,
|
|
248 double (**func2)(void *, double, double), char **func2_name,
|
|
249 void *opaque, char **error){
|
|
250 Parser p;
|
|
251
|
|
252 p.stack_index=100;
|
|
253 p.s= s;
|
|
254 p.const_value= const_value;
|
|
255 p.const_name = const_name;
|
|
256 p.func1 = func1;
|
|
257 p.func1_name = func1_name;
|
|
258 p.func2 = func2;
|
|
259 p.func2_name = func2_name;
|
|
260 p.opaque = opaque;
|
|
261 p.error= error;
|
|
262
|
|
263 return evalExpression(&p);
|
|
264 }
|
|
265
|
|
266 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
|
|
267 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
|
|
268 double (**func1)(void *, double), const char **func1_name,
|
|
269 double (**func2)(void *, double, double), char **func2_name,
|
|
270 void *opaque){
|
|
271 char *error=NULL;
|
|
272 double ret;
|
|
273 ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
|
|
274 if (error)
|
|
275 av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
|
|
276 return ret;
|
|
277 }
|
|
278 #endif
|
|
279
|
|
280 #ifdef TEST
|
|
281 #undef printf
|
|
282 static double const_values[]={
|
|
283 M_PI,
|
|
284 M_E,
|
|
285 0
|
|
286 };
|
|
287 static const char *const_names[]={
|
|
288 "PI",
|
|
289 "E",
|
|
290 0
|
|
291 };
|
|
292 main(){
|
|
293 int i;
|
|
294 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));
|
|
295 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
|
|
296
|
|
297 for(i=0; i<1050; i++){
|
|
298 START_TIMER
|
|
299 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);
|
|
300 STOP_TIMER("ff_eval")
|
|
301 }
|
|
302 }
|
|
303 #endif
|