Mercurial > libavcodec.hg
annotate eval.c @ 1064:b32afefe7d33 libavcodec
* UINTX -> uintx_t INTX -> intx_t
| author | kabi |
|---|---|
| date | Tue, 11 Feb 2003 16:35:48 +0000 |
| parents | bb5de8a59da8 |
| children | 1e39f273ecd6 |
| rev | line source |
|---|---|
| 612 | 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 * see http://joe.hotchkiss.com/programming/eval/eval.html | |
| 24 */ | |
| 25 | |
| 1057 | 26 #include "avcodec.h" |
| 27 #include "mpegvideo.h" | |
| 28 | |
| 612 | 29 #include <stdio.h> |
| 30 #include <stdlib.h> | |
| 31 #include <string.h> | |
| 32 #include <math.h> | |
| 33 | |
|
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
34 #ifndef NAN |
|
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
35 #define NAN 0 |
|
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
36 #endif |
|
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
37 |
| 627 | 38 #ifndef M_PI |
| 39 #define M_PI 3.14159265358979323846 | |
| 40 #endif | |
| 41 | |
| 612 | 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; | |
| 1057 | 49 const char **const_name; // NULL terminated |
| 612 | 50 double (**func1)(void *, double a); // NULL terminated |
| 1057 | 51 const char **func1_name; // NULL terminated |
| 612 | 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 | |
| 1057 | 77 static int strmatch(const char *s, const char *prefix){ |
| 612 | 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 p->s++; // ")" or "," | |
| 116 if(p->s[-1]== ','){ | |
| 117 evalExpression(p); | |
| 118 d2= pop(p); | |
| 119 p->s++; // ")" | |
| 120 } | |
| 121 | |
| 122 if( strmatch(next, "sinh" ) ) d= sinh(d); | |
| 123 else if( strmatch(next, "cosh" ) ) d= cosh(d); | |
| 124 else if( strmatch(next, "tanh" ) ) d= tanh(d); | |
| 125 else if( strmatch(next, "sin" ) ) d= sin(d); | |
| 126 else if( strmatch(next, "cos" ) ) d= cos(d); | |
| 127 else if( strmatch(next, "tan" ) ) d= tan(d); | |
| 128 else if( strmatch(next, "exp" ) ) d= exp(d); | |
| 129 else if( strmatch(next, "log" ) ) d= log(d); | |
| 130 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d)); | |
| 131 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI); | |
| 1057 | 132 else if( strmatch(next, "abs" ) ) d= fabs(d); |
| 612 | 133 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2; |
| 134 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2; | |
| 135 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; | |
| 136 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; | |
| 137 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; | |
| 138 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); | |
| 139 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; | |
| 140 else{ | |
| 141 int error=1; | |
| 142 for(i=0; p->func1_name && p->func1_name[i]; i++){ | |
| 143 if(strmatch(next, p->func1_name[i])){ | |
| 144 d= p->func1[i](p->opaque, d); | |
| 145 error=0; | |
| 146 break; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
| 151 if(strmatch(next, p->func2_name[i])){ | |
| 152 d= p->func2[i](p->opaque, d, d2); | |
| 153 error=0; | |
| 154 break; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 if(error){ | |
| 159 fprintf(stderr, "Parser: unknown function in \"%s\"\n", next); | |
| 160 return; | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 if(p->s[-1]!= ')'){ | |
| 165 fprintf(stderr, "Parser: missing ) in \"%s\"\n", next); | |
| 166 return; | |
| 167 } | |
| 168 push(p, d); | |
| 169 } | |
| 170 | |
| 171 static void evalPow(Parser *p){ | |
| 172 int neg= 0; | |
| 173 if(p->s[0]=='+') p->s++; | |
| 174 | |
| 175 if(p->s[0]=='-'){ | |
| 176 neg= 1; | |
| 177 p->s++; | |
| 178 } | |
| 179 | |
| 180 if(p->s[0]=='('){ | |
| 181 p->s++;; | |
| 182 evalExpression(p); | |
| 183 | |
| 184 if(p->s[0]!=')') | |
| 185 fprintf(stderr, "Parser: missing )\n"); | |
| 186 p->s++; | |
| 187 }else{ | |
| 188 evalPrimary(p); | |
| 189 } | |
| 190 | |
| 191 if(neg) push(p, -pop(p)); | |
| 192 } | |
| 193 | |
| 194 static void evalFactor(Parser *p){ | |
| 195 evalPow(p); | |
| 196 while(p->s[0]=='^'){ | |
| 197 double d; | |
| 198 | |
| 199 p->s++; | |
| 200 evalPow(p); | |
| 201 d= pop(p); | |
| 202 push(p, pow(pop(p), d)); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 static void evalTerm(Parser *p){ | |
| 207 evalFactor(p); | |
| 208 while(p->s[0]=='*' || p->s[0]=='/'){ | |
| 209 int inv= p->s[0]=='/'; | |
| 210 double d; | |
| 211 | |
| 212 p->s++; | |
| 213 evalFactor(p); | |
| 214 d= pop(p); | |
| 215 if(inv) d= 1.0/d; | |
| 216 push(p, d * pop(p)); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 static void evalExpression(Parser *p){ | |
| 221 evalTerm(p); | |
| 222 while(p->s[0]=='+' || p->s[0]=='-'){ | |
| 223 int sign= p->s[0]=='-'; | |
| 224 double d; | |
| 225 | |
| 226 p->s++; | |
| 227 evalTerm(p); | |
| 228 d= pop(p); | |
| 229 if(sign) d= -d; | |
| 230 push(p, d + pop(p)); | |
| 231 } | |
| 232 } | |
| 233 | |
| 1057 | 234 double ff_eval(char *s, double *const_value, const char **const_name, |
| 235 double (**func1)(void *, double), const char **func1_name, | |
| 612 | 236 double (**func2)(void *, double, double), char **func2_name, |
| 237 void *opaque){ | |
| 238 Parser p; | |
| 239 | |
| 240 p.stack_index=0; | |
| 241 p.s= s; | |
| 242 p.const_value= const_value; | |
| 243 p.const_name = const_name; | |
| 244 p.func1 = func1; | |
| 245 p.func1_name = func1_name; | |
| 246 p.func2 = func2; | |
| 247 p.func2_name = func2_name; | |
| 248 p.opaque = opaque; | |
| 249 | |
| 250 evalExpression(&p); | |
| 251 return pop(&p); | |
| 252 } |
