]> git.sesse.net Git - ffmpeg/blob - libavcodec/eval.c
fix a warning
[ffmpeg] / libavcodec / eval.c
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21
22 /**
23  * @file eval.c
24  * simple arithmetic expression evaluator.
25  *
26  * see http://joe.hotchkiss.com/programming/eval/eval.html
27  */
28
29 #include "avcodec.h"
30 #include "mpegvideo.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #ifndef NAN
38   #define NAN 0.0/0.0
39 #endif
40
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846
43 #endif
44
45 typedef struct Parser{
46     int stack_index;
47     char *s;
48     double *const_value;
49     const char **const_name;          // NULL terminated
50     double (**func1)(void *, double a); // NULL terminated
51     const char **func1_name;          // NULL terminated
52     double (**func2)(void *, double a, double b); // NULL terminated
53     char **func2_name;          // NULL terminated
54     void *opaque;
55     char **error;
56 } Parser;
57
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 };
82
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     char *next;
90     d = strtod(name, &next);
91     /* if parsing succeeded, check for and interpret postfixes */
92     if (next!=name) {
93
94         if(*next >= 'E' && *next <= 'z'){
95             int e= si_prefixes[*next - 'E'];
96             if(e){
97                 if(next[1] == 'i'){
98                     d*= pow( 2, e/0.3);
99                     next+=2;
100                 }else{
101                     d*= pow(10, e);
102                     next++;
103                 }
104             }
105         }
106
107         if(*next=='B') {
108             d*=8;
109             *next++;
110         }
111     }
112     /* if requested, fill in tail with the position after the last parsed
113        character */
114     if (tail)
115         *tail = next;
116     return d;
117 }
118
119 static int strmatch(const char *s, const char *prefix){
120     int i;
121     for(i=0; prefix[i]; i++){
122         if(prefix[i] != s[i]) return 0;
123     }
124     return 1;
125 }
126
127 static double evalPrimary(Parser *p){
128     double d, d2=NAN;
129     char *next= p->s;
130     int i;
131
132     /* number */
133     d= av_strtod(p->s, &next);
134     if(next != p->s){
135         p->s= next;
136         return d;
137     }
138
139     /* named constants */
140     for(i=0; p->const_name && p->const_name[i]; i++){
141         if(strmatch(p->s, p->const_name[i])){
142             p->s+= strlen(p->const_name[i]);
143             return p->const_value[i];
144         }
145     }
146
147     p->s= strchr(p->s, '(');
148     if(p->s==NULL){
149         *p->error = "missing (";
150         p->s= next;
151         return NAN;
152     }
153     p->s++; // "("
154     d= evalExpression(p);
155     if(p->s[0]== ','){
156         p->s++; // ","
157         d2= evalExpression(p);
158     }
159     if(p->s[0] != ')'){
160         *p->error = "missing )";
161         return NAN;
162     }
163     p->s++; // ")"
164
165          if( strmatch(next, "sinh"  ) ) d= sinh(d);
166     else if( strmatch(next, "cosh"  ) ) d= cosh(d);
167     else if( strmatch(next, "tanh"  ) ) d= tanh(d);
168     else if( strmatch(next, "sin"   ) ) d= sin(d);
169     else if( strmatch(next, "cos"   ) ) d= cos(d);
170     else if( strmatch(next, "tan"   ) ) d= tan(d);
171     else if( strmatch(next, "exp"   ) ) d= exp(d);
172     else if( strmatch(next, "log"   ) ) d= log(d);
173     else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
174     else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
175     else if( strmatch(next, "abs"   ) ) d= fabs(d);
176     else if( strmatch(next, "max"   ) ) d= d >  d2 ?   d : d2;
177     else if( strmatch(next, "min"   ) ) d= d <  d2 ?   d : d2;
178     else if( strmatch(next, "gt"    ) ) d= d >  d2 ? 1.0 : 0.0;
179     else if( strmatch(next, "gte"   ) ) d= d >= d2 ? 1.0 : 0.0;
180     else if( strmatch(next, "lt"    ) ) d= d >  d2 ? 0.0 : 1.0;
181     else if( strmatch(next, "lte"   ) ) d= d >= d2 ? 0.0 : 1.0;
182     else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
183     else if( strmatch(next, "("     ) ) d= d;
184 //    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
185 //    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
186     else{
187         for(i=0; p->func1_name && p->func1_name[i]; i++){
188             if(strmatch(next, p->func1_name[i])){
189                 return p->func1[i](p->opaque, d);
190             }
191         }
192
193         for(i=0; p->func2_name && p->func2_name[i]; i++){
194             if(strmatch(next, p->func2_name[i])){
195                 return p->func2[i](p->opaque, d, d2);
196             }
197         }
198
199         *p->error = "unknown function";
200         return NAN;
201     }
202
203     return d;
204 }
205
206 static double evalPow(Parser *p){
207     int sign= (*p->s == '+') - (*p->s == '-');
208     p->s += sign&1;
209     return (sign|1) * evalPrimary(p);
210 }
211
212 static double evalFactor(Parser *p){
213     double ret= evalPow(p);
214     while(p->s[0]=='^'){
215         p->s++;
216         ret= pow(ret, evalPow(p));
217     }
218     return 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