Mercurial > libavcodec.hg
annotate eval.c @ 3755:882cf925ff7a libavcodec
cosmetic
| author | michael |
|---|---|
| date | Sun, 24 Sep 2006 10:41:25 +0000 |
| parents | 6f219b6839ba |
| children | 9813c594dac5 |
| 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 | |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 612 | 19 * |
| 20 */ | |
| 21 | |
| 1106 | 22 /** |
| 23 * @file eval.c | |
| 24 * simple arithmetic expression evaluator. | |
| 25 * | |
| 612 | 26 * see http://joe.hotchkiss.com/programming/eval/eval.html |
| 27 */ | |
| 28 | |
| 1057 | 29 #include "avcodec.h" |
| 30 #include "mpegvideo.h" | |
| 31 | |
| 612 | 32 #include <stdio.h> |
| 33 #include <stdlib.h> | |
| 34 #include <string.h> | |
| 35 #include <math.h> | |
| 36 | |
|
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
37 #ifndef NAN |
| 3753 | 38 #define NAN 0.0/0.0 |
|
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
39 #endif |
|
b786f15df503
NAN doesnt exist on FreeBSD patch by (R?mi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
40 |
| 627 | 41 #ifndef M_PI |
| 42 #define M_PI 3.14159265358979323846 | |
| 43 #endif | |
| 44 | |
| 612 | 45 typedef struct Parser{ |
| 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 | |
| 2434 | 57 static double evalExpression(Parser *p); |
| 612 | 58 |
| 1057 | 59 static int strmatch(const char *s, const char *prefix){ |
| 612 | 60 int i; |
| 61 for(i=0; prefix[i]; i++){ | |
| 62 if(prefix[i] != s[i]) return 0; | |
| 63 } | |
| 64 return 1; | |
| 65 } | |
| 66 | |
| 2434 | 67 static double evalPrimary(Parser *p){ |
| 612 | 68 double d, d2=NAN; |
| 69 char *next= p->s; | |
| 70 int i; | |
| 71 | |
| 72 /* number */ | |
|
3731
8b8773577dd9
Add support for SI (k, M, ...) and IEC/IEEE (Ki, Mi, ...) units.
takis
parents:
3730
diff
changeset
|
73 d= av_strtod(p->s, &next); |
| 612 | 74 if(next != p->s){ |
| 75 p->s= next; | |
| 2434 | 76 return d; |
| 612 | 77 } |
| 2967 | 78 |
| 612 | 79 /* named constants */ |
| 2433 | 80 for(i=0; p->const_name && p->const_name[i]; i++){ |
| 612 | 81 if(strmatch(p->s, p->const_name[i])){ |
| 82 p->s+= strlen(p->const_name[i]); | |
| 2434 | 83 return p->const_value[i]; |
| 612 | 84 } |
| 85 } | |
| 2967 | 86 |
| 612 | 87 p->s= strchr(p->s, '('); |
| 88 if(p->s==NULL){ | |
|
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1106
diff
changeset
|
89 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next); |
| 3754 | 90 p->s= next; |
| 2434 | 91 return NAN; |
| 612 | 92 } |
| 93 p->s++; // "(" | |
| 2434 | 94 d= evalExpression(p); |
| 1815 | 95 if(p->s[0]== ','){ |
| 96 p->s++; // "," | |
| 2434 | 97 d2= evalExpression(p); |
| 612 | 98 } |
| 1815 | 99 if(p->s[0] != ')'){ |
| 100 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next); | |
| 2434 | 101 return NAN; |
| 1815 | 102 } |
| 103 p->s++; // ")" | |
| 2967 | 104 |
| 612 | 105 if( strmatch(next, "sinh" ) ) d= sinh(d); |
| 106 else if( strmatch(next, "cosh" ) ) d= cosh(d); | |
| 107 else if( strmatch(next, "tanh" ) ) d= tanh(d); | |
| 108 else if( strmatch(next, "sin" ) ) d= sin(d); | |
| 109 else if( strmatch(next, "cos" ) ) d= cos(d); | |
| 110 else if( strmatch(next, "tan" ) ) d= tan(d); | |
| 111 else if( strmatch(next, "exp" ) ) d= exp(d); | |
| 112 else if( strmatch(next, "log" ) ) d= log(d); | |
| 113 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d)); | |
| 114 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI); | |
| 1057 | 115 else if( strmatch(next, "abs" ) ) d= fabs(d); |
| 3755 | 116 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2; |
| 117 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2; | |
| 118 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; | |
| 119 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0; | |
| 120 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; | |
| 121 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; | |
| 612 | 122 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; |
| 2434 | 123 else if( strmatch(next, "(" ) ) d= d; |
| 612 | 124 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); |
| 125 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; | |
| 126 else{ | |
| 127 for(i=0; p->func1_name && p->func1_name[i]; i++){ | |
| 128 if(strmatch(next, p->func1_name[i])){ | |
| 2434 | 129 return p->func1[i](p->opaque, d); |
| 612 | 130 } |
| 131 } | |
| 132 | |
| 133 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
| 134 if(strmatch(next, p->func2_name[i])){ | |
| 2434 | 135 return p->func2[i](p->opaque, d, d2); |
| 612 | 136 } |
| 137 } | |
| 138 | |
| 2433 | 139 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next); |
| 2434 | 140 return NAN; |
| 612 | 141 } |
| 2433 | 142 |
| 2434 | 143 return d; |
| 2967 | 144 } |
| 2436 | 145 |
| 2434 | 146 static double evalPow(Parser *p){ |
| 2436 | 147 int sign= (*p->s == '+') - (*p->s == '-'); |
| 148 p->s += sign&1; | |
| 149 return (sign|1) * evalPrimary(p); | |
| 2434 | 150 } |
| 612 | 151 |
| 2434 | 152 static double evalFactor(Parser *p){ |
| 153 double ret= evalPow(p); | |
| 154 while(p->s[0]=='^'){ | |
| 612 | 155 p->s++; |
| 2434 | 156 ret= pow(ret, evalPow(p)); |
| 612 | 157 } |
| 2434 | 158 return ret; |
| 612 | 159 } |
| 160 | |
| 2434 | 161 static double evalTerm(Parser *p){ |
| 162 double ret= evalFactor(p); | |
| 163 while(p->s[0]=='*' || p->s[0]=='/'){ | |
| 164 if(*p->s++ == '*') ret*= evalFactor(p); | |
| 165 else ret/= evalFactor(p); | |
| 612 | 166 } |
| 2434 | 167 return ret; |
| 612 | 168 } |
| 169 | |
| 2434 | 170 static double evalExpression(Parser *p){ |
| 2436 | 171 double ret= 0; |
| 612 | 172 |
| 2434 | 173 if(p->stack_index <= 0) //protect against stack overflows |
| 174 return NAN; | |
| 175 p->stack_index--; | |
| 176 | |
| 2436 | 177 do{ |
| 178 ret += evalTerm(p); | |
| 179 }while(*p->s == '+' || *p->s == '-'); | |
| 612 | 180 |
| 2434 | 181 p->stack_index++; |
| 612 | 182 |
| 2434 | 183 return ret; |
| 612 | 184 } |
| 185 | |
| 1057 | 186 double ff_eval(char *s, double *const_value, const char **const_name, |
| 187 double (**func1)(void *, double), const char **func1_name, | |
| 612 | 188 double (**func2)(void *, double, double), char **func2_name, |
| 189 void *opaque){ | |
| 190 Parser p; | |
| 2967 | 191 |
| 2434 | 192 p.stack_index=100; |
| 612 | 193 p.s= s; |
| 194 p.const_value= const_value; | |
| 195 p.const_name = const_name; | |
| 196 p.func1 = func1; | |
| 197 p.func1_name = func1_name; | |
| 198 p.func2 = func2; | |
| 199 p.func2_name = func2_name; | |
| 200 p.opaque = opaque; | |
| 2967 | 201 |
| 2434 | 202 return evalExpression(&p); |
| 612 | 203 } |
| 2433 | 204 |
| 205 #ifdef TEST | |
| 2967 | 206 #undef printf |
| 2433 | 207 static double const_values[]={ |
| 208 M_PI, | |
| 209 M_E, | |
| 210 0 | |
| 211 }; | |
| 212 static const char *const_names[]={ | |
| 213 "PI", | |
| 214 "E", | |
| 215 0 | |
| 216 }; | |
| 217 main(){ | |
| 2436 | 218 int i; |
| 2433 | 219 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)); |
| 3730 | 220 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL)); |
| 2967 | 221 |
| 2436 | 222 for(i=0; i<1050; i++){ |
| 223 START_TIMER | |
| 224 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); | |
| 225 STOP_TIMER("ff_eval") | |
| 226 } | |
| 2433 | 227 } |
| 228 #endif |
