]> git.sesse.net Git - x264/blob - encoder/eval.c
Forgot rbsp_trailing_bits in AUD NAL
[x264] / encoder / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #ifndef NAN
35   #define NAN 0
36 #endif
37
38 #ifndef M_PI
39 #define M_PI 3.14159265358979323846
40 #endif
41
42 #define STACK_SIZE 100
43
44 typedef struct Parser{
45     double stack[STACK_SIZE];
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 } Parser;
56
57 static void evalExpression(Parser *p);
58
59 static void push(Parser *p, double d){
60     if(p->stack_index+1>= STACK_SIZE){
61         fprintf(stderr, "stack overflow in the parser\n");
62         return;
63     }
64     p->stack[ p->stack_index++ ]= d;
65 //printf("push %f\n", d); fflush(stdout);
66 }
67
68 static double pop(Parser *p){
69     if(p->stack_index<=0){
70         fprintf(stderr, "stack underflow in the parser\n");
71         return NAN;
72     }
73 //printf("pop\n"); fflush(stdout);
74     return p->stack[ --p->stack_index ];
75 }
76
77 static int strmatch(const char *s, const char *prefix){
78     int i;
79     for(i=0; prefix[i]; i++){
80         if(prefix[i] != s[i]) return 0;
81     }
82     return 1;
83 }
84
85 static void evalPrimary(Parser *p){
86     double d, d2=NAN;
87     char *next= p->s;
88     int i;
89
90     /* number */
91     d= strtod(p->s, &next);
92     if(next != p->s){
93         push(p, d);
94         p->s= next;
95         return;
96     }
97
98     /* named constants */
99     for(i=0; p->const_name[i]; i++){
100         if(strmatch(p->s, p->const_name[i])){
101             push(p, p->const_value[i]);
102             p->s+= strlen(p->const_name[i]);
103             return;
104         }
105     }
106
107     p->s= strchr(p->s, '(');
108     if(p->s==NULL){
109         fprintf(stderr, "Parser: missing ( in \"%s\"\n", next);
110         return;
111     }
112     p->s++; // "("
113     evalExpression(p);
114     d= pop(p);
115     if(p->s[0]== ','){
116         p->s++; // ","
117         evalExpression(p);
118         d2= pop(p);
119     }
120     if(p->s[0] != ')'){
121         fprintf(stderr, "Parser: missing ) in \"%s\"\n", next);
122         return;
123     }
124     p->s++; // ")"
125
126          if( strmatch(next, "sinh"  ) ) d= sinh(d);
127     else if( strmatch(next, "cosh"  ) ) d= cosh(d);
128     else if( strmatch(next, "tanh"  ) ) d= tanh(d);
129     else if( strmatch(next, "sin"   ) ) d= sin(d);
130     else if( strmatch(next, "cos"   ) ) d= cos(d);
131     else if( strmatch(next, "tan"   ) ) d= tan(d);
132     else if( strmatch(next, "exp"   ) ) d= exp(d);
133     else if( strmatch(next, "log"   ) ) d= log(d);
134     else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
135     else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
136     else if( strmatch(next, "abs"   ) ) d= fabs(d);
137     else if( strmatch(next, "max"   ) ) d= d > d2 ? d : d2;
138     else if( strmatch(next, "min"   ) ) d= d < d2 ? d : d2;
139     else if( strmatch(next, "gt"    ) ) d= d > d2 ? 1.0 : 0.0;
140     else if( strmatch(next, "gte"    ) ) d= d >= d2 ? 1.0 : 0.0;
141     else if( strmatch(next, "lt"    ) ) d= d > d2 ? 0.0 : 1.0;
142     else if( strmatch(next, "lte"    ) ) d= d >= d2 ? 0.0 : 1.0;
143     else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
144 //    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
145 //    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
146     else{
147         int error=1;
148         for(i=0; p->func1_name && p->func1_name[i]; i++){
149             if(strmatch(next, p->func1_name[i])){
150                 d= p->func1[i](p->opaque, d);
151                 error=0;
152                 break;
153             }
154         }
155
156         for(i=0; p->func2_name && p->func2_name[i]; i++){
157             if(strmatch(next, p->func2_name[i])){
158                 d= p->func2[i](p->opaque, d, d2);
159                 error=0;
160                 break;
161             }
162         }
163
164         if(error){
165             fprintf(stderr, "Parser: unknown function in \"%s\"\n", next);
166             return;
167         }
168     }
169
170     push(p, d);
171 }
172
173 static void evalPow(Parser *p){
174     int neg= 0;
175     if(p->s[0]=='+') p->s++;
176
177     if(p->s[0]=='-'){
178         neg= 1;
179         p->s++;
180     }
181
182     if(p->s[0]=='('){
183         p->s++;;
184         evalExpression(p);
185
186         if(p->s[0]!=')')
187             fprintf(stderr, "Parser: missing )\n");
188         p->s++;
189     }else{
190         evalPrimary(p);
191     }
192
193     if(neg) push(p, -pop(p));
194 }
195
196 static void evalFactor(Parser *p){
197     evalPow(p);
198     while(p->s[0]=='^'){
199         double d;
200
201         p->s++;
202         evalPow(p);
203         d= pop(p);
204         push(p, pow(pop(p), d));
205     }
206 }
207
208 static void evalTerm(Parser *p){
209     evalFactor(p);
210     while(p->s[0]=='*' || p->s[0]=='/'){
211         int inv= p->s[0]=='/';
212         double d;
213
214         p->s++;
215         evalFactor(p);
216         d= pop(p);
217         if(inv) d= 1.0/d;
218         push(p, d * pop(p));
219     }
220 }
221
222 static void evalExpression(Parser *p){
223     evalTerm(p);
224     while(p->s[0]=='+' || p->s[0]=='-'){
225         int sign= p->s[0]=='-';
226         double d;
227
228         p->s++;
229         evalTerm(p);
230         d= pop(p);
231         if(sign) d= -d;
232         push(p, d + pop(p));
233     }
234 }
235
236 double x264_eval(char *s, double *const_value, const char **const_name,
237                  double (**func1)(void *, double), const char **func1_name,
238                  double (**func2)(void *, double, double), char **func2_name,
239                  void *opaque){
240     Parser p;
241
242     p.stack_index=0;
243     p.s= s;
244     p.const_value= const_value;
245     p.const_name = const_name;
246     p.func1      = func1;
247     p.func1_name = func1_name;
248     p.func2      = func2;
249     p.func2_name = func2_name;
250     p.opaque     = opaque;
251
252     evalExpression(&p);
253     return pop(&p);
254 }