]> git.sesse.net Git - ffmpeg/blob - libavcodec/eval.c
Break compatibility only when first part of version number changes, in this
[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     int p = 0;
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, "exp"   ) ) d= exp(d);
173     else if( strmatch(next, "log"   ) ) d= log(d);
174     else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
175     else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
176     else if( strmatch(next, "abs"   ) ) d= fabs(d);
177     else if( strmatch(next, "max"   ) ) d= d >  d2 ?   d : d2;
178     else if( strmatch(next, "min"   ) ) d= d <  d2 ?   d : d2;
179     else if( strmatch(next, "gt"    ) ) d= d >  d2 ? 1.0 : 0.0;
180     else if( strmatch(next, "gte"   ) ) d= d >= d2 ? 1.0 : 0.0;
181     else if( strmatch(next, "lt"    ) ) d= d >  d2 ? 0.0 : 1.0;
182     else if( strmatch(next, "lte"   ) ) d= d >= d2 ? 0.0 : 1.0;
183     else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
184     else if( strmatch(next, "("     ) ) d= d;
185 //    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
186 //    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
187     else{
188         for(i=0; p->func1_name && p->func1_name[i]; i++){
189             if(strmatch(next, p->func1_name[i])){
190                 return p->func1[i](p->opaque, d);
191             }
192         }
193
194         for(i=0; p->func2_name && p->func2_name[i]; i++){
195             if(strmatch(next, p->func2_name[i])){
196                 return p->func2[i](p->opaque, d, d2);
197             }
198         }
199
200         *p->error = "unknown function";
201         return NAN;
202     }
203
204     return d;
205 }
206
207 static double evalPow(Parser *p){
208     int sign= (*p->s == '+') - (*p->s == '-');
209     p->s += sign&1;
210     return (sign|1) * evalPrimary(p);
211 }
212
213 static double evalFactor(Parser *p){
214     double ret= evalPow(p);
215     while(p->s[0]=='^'){
216         p->s++;
217         ret= pow(ret, evalPow(p));
218     }
219     return ret;
220 }
221
222 static double evalTerm(Parser *p){
223     double ret= evalFactor(p);
224     while(p->s[0]=='*' || p->s[0]=='/'){
225         if(*p->s++ == '*') ret*= evalFactor(p);
226         else               ret/= evalFactor(p);
227     }
228     return ret;
229 }
230
231 static double evalExpression(Parser *p){
232     double ret= 0;
233
234     if(p->stack_index <= 0) //protect against stack overflows
235         return NAN;
236     p->stack_index--;
237
238     do{
239         ret += evalTerm(p);
240     }while(*p->s == '+' || *p->s == '-');
241
242     p->stack_index++;
243
244     return ret;
245 }
246
247 double ff_eval2(char *s, double *const_value, const char **const_name,
248                double (**func1)(void *, double), const char **func1_name,
249                double (**func2)(void *, double, double), char **func2_name,
250                void *opaque, char **error){
251     Parser p;
252
253     p.stack_index=100;
254     p.s= s;
255     p.const_value= const_value;
256     p.const_name = const_name;
257     p.func1      = func1;
258     p.func1_name = func1_name;
259     p.func2      = func2;
260     p.func2_name = func2_name;
261     p.opaque     = opaque;
262     p.error= error;
263
264     return evalExpression(&p);
265 }
266
267 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
268 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
269                double (**func1)(void *, double), const char **func1_name,
270                double (**func2)(void *, double, double), char **func2_name,
271                void *opaque){
272     char *error=NULL;
273     double ret;
274     ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
275     if (error)
276         av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
277     return ret;
278 }
279 #endif
280
281 #ifdef TEST
282 #undef printf
283 static double const_values[]={
284     M_PI,
285     M_E,
286     0
287 };
288 static const char *const_names[]={
289     "PI",
290     "E",
291     0
292 };
293 main(){
294     int i;
295     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));
296     printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
297
298     for(i=0; i<1050; i++){
299         START_TIMER
300             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);
301         STOP_TIMER("ff_eval")
302     }
303 }
304 #endif