00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00031 #include "avcodec.h"
00032 #include "mpegvideo.h"
00033 #include "eval.h"
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <math.h>
00039
00040 #ifndef NAN
00041 #define NAN 0.0/0.0
00042 #endif
00043
00044 #ifndef M_PI
00045 #define M_PI 3.14159265358979323846
00046 #endif
00047
00048 typedef struct Parser{
00049 int stack_index;
00050 char *s;
00051 double *const_value;
00052 const char **const_name;
00053 double (**func1)(void *, double a);
00054 const char **func1_name;
00055 double (**func2)(void *, double a, double b);
00056 char **func2_name;
00057 void *opaque;
00058 const char **error;
00059 #define VARS 10
00060 double var[VARS];
00061 } Parser;
00062
00063 static int8_t si_prefixes['z' - 'E' + 1]={
00064 ['y'-'E']= -24,
00065 ['z'-'E']= -21,
00066 ['a'-'E']= -18,
00067 ['f'-'E']= -15,
00068 ['p'-'E']= -12,
00069 ['n'-'E']= - 9,
00070 ['u'-'E']= - 6,
00071 ['m'-'E']= - 3,
00072 ['c'-'E']= - 2,
00073 ['d'-'E']= - 1,
00074 ['h'-'E']= 2,
00075 ['k'-'E']= 3,
00076 ['K'-'E']= 3,
00077 ['M'-'E']= 6,
00078 ['G'-'E']= 9,
00079 ['T'-'E']= 12,
00080 ['P'-'E']= 15,
00081 ['E'-'E']= 18,
00082 ['Z'-'E']= 21,
00083 ['Y'-'E']= 24,
00084 };
00085
00090 static double av_strtod(const char *name, char **tail) {
00091 double d;
00092 char *next;
00093 d = strtod(name, &next);
00094
00095 if (next!=name) {
00096
00097 if(*next >= 'E' && *next <= 'z'){
00098 int e= si_prefixes[*next - 'E'];
00099 if(e){
00100 if(next[1] == 'i'){
00101 d*= pow( 2, e/0.3);
00102 next+=2;
00103 }else{
00104 d*= pow(10, e);
00105 next++;
00106 }
00107 }
00108 }
00109
00110 if(*next=='B') {
00111 d*=8;
00112 next++;
00113 }
00114 }
00115
00116
00117 if (tail)
00118 *tail = next;
00119 return d;
00120 }
00121
00122 static int strmatch(const char *s, const char *prefix){
00123 int i;
00124 for(i=0; prefix[i]; i++){
00125 if(prefix[i] != s[i]) return 0;
00126 }
00127 return 1;
00128 }
00129
00130 struct ff_expr_s {
00131 enum {
00132 e_value, e_const, e_func0, e_func1, e_func2,
00133 e_squish, e_gauss, e_ld,
00134 e_mod, e_max, e_min, e_eq, e_gt, e_gte,
00135 e_pow, e_mul, e_div, e_add,
00136 e_last, e_st, e_while,
00137 } type;
00138 double value;
00139 union {
00140 int const_index;
00141 double (*func0)(double);
00142 double (*func1)(void *, double);
00143 double (*func2)(void *, double, double);
00144 } a;
00145 AVEvalExpr * param[2];
00146 };
00147
00148 static double eval_expr(Parser * p, AVEvalExpr * e) {
00149 switch (e->type) {
00150 case e_value: return e->value;
00151 case e_const: return e->value * p->const_value[e->a.const_index];
00152 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
00153 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
00154 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
00155 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
00156 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
00157 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
00158 case e_while: {
00159 double d = NAN;
00160 while(eval_expr(p, e->param[0]))
00161 d=eval_expr(p, e->param[1]);
00162 return d;
00163 }
00164 default: {
00165 double d = eval_expr(p, e->param[0]);
00166 double d2 = eval_expr(p, e->param[1]);
00167 switch (e->type) {
00168 case e_mod: return e->value * (d - floor(d/d2)*d2);
00169 case e_max: return e->value * (d > d2 ? d : d2);
00170 case e_min: return e->value * (d < d2 ? d : d2);
00171 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
00172 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
00173 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
00174 case e_pow: return e->value * pow(d, d2);
00175 case e_mul: return e->value * (d * d2);
00176 case e_div: return e->value * (d / d2);
00177 case e_add: return e->value * (d + d2);
00178 case e_last:return e->value * d2;
00179 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
00180 }
00181 }
00182 }
00183 return NAN;
00184 }
00185
00186 static AVEvalExpr * parse_expr(Parser *p);
00187
00188 void ff_eval_free(AVEvalExpr * e) {
00189 if (!e) return;
00190 ff_eval_free(e->param[0]);
00191 ff_eval_free(e->param[1]);
00192 av_freep(&e);
00193 }
00194
00195 static AVEvalExpr * parse_primary(Parser *p) {
00196 AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr));
00197 char *next= p->s;
00198 int i;
00199
00200
00201 d->value = av_strtod(p->s, &next);
00202 if(next != p->s){
00203 d->type = e_value;
00204 p->s= next;
00205 return d;
00206 }
00207 d->value = 1;
00208
00209
00210 for(i=0; p->const_name && p->const_name[i]; i++){
00211 if(strmatch(p->s, p->const_name[i])){
00212 p->s+= strlen(p->const_name[i]);
00213 d->type = e_const;
00214 d->a.const_index = i;
00215 return d;
00216 }
00217 }
00218
00219 p->s= strchr(p->s, '(');
00220 if(p->s==NULL){
00221 *p->error = "missing (";
00222 p->s= next;
00223 ff_eval_free(d);
00224 return NULL;
00225 }
00226 p->s++;
00227 if (*next == '(') {
00228 av_freep(&d);
00229 d = parse_expr(p);
00230 if(p->s[0] != ')'){
00231 *p->error = "missing )";
00232 ff_eval_free(d);
00233 return NULL;
00234 }
00235 p->s++;
00236 return d;
00237 }
00238 d->param[0] = parse_expr(p);
00239 if(p->s[0]== ','){
00240 p->s++;
00241 d->param[1] = parse_expr(p);
00242 }
00243 if(p->s[0] != ')'){
00244 *p->error = "missing )";
00245 ff_eval_free(d);
00246 return NULL;
00247 }
00248 p->s++;
00249
00250 d->type = e_func0;
00251 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
00252 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
00253 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
00254 else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
00255 else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
00256 else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
00257 else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
00258 else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
00259 else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
00260 else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
00261 else if( strmatch(next, "log" ) ) d->a.func0 = log;
00262 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
00263 else if( strmatch(next, "squish") ) d->type = e_squish;
00264 else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
00265 else if( strmatch(next, "mod" ) ) d->type = e_mod;
00266 else if( strmatch(next, "max" ) ) d->type = e_max;
00267 else if( strmatch(next, "min" ) ) d->type = e_min;
00268 else if( strmatch(next, "eq" ) ) d->type = e_eq;
00269 else if( strmatch(next, "gte" ) ) d->type = e_gte;
00270 else if( strmatch(next, "gt" ) ) d->type = e_gt;
00271 else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
00272 else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
00273 else if( strmatch(next, "ld" ) ) d->type = e_ld;
00274 else if( strmatch(next, "st" ) ) d->type = e_st;
00275 else if( strmatch(next, "while" ) ) d->type = e_while;
00276 else {
00277 for(i=0; p->func1_name && p->func1_name[i]; i++){
00278 if(strmatch(next, p->func1_name[i])){
00279 d->a.func1 = p->func1[i];
00280 d->type = e_func1;
00281 return d;
00282 }
00283 }
00284
00285 for(i=0; p->func2_name && p->func2_name[i]; i++){
00286 if(strmatch(next, p->func2_name[i])){
00287 d->a.func2 = p->func2[i];
00288 d->type = e_func2;
00289 return d;
00290 }
00291 }
00292
00293 *p->error = "unknown function";
00294 ff_eval_free(d);
00295 return NULL;
00296 }
00297
00298 return d;
00299 }
00300
00301 static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){
00302 AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr));
00303 e->type =type ;
00304 e->value =value ;
00305 e->param[0] =p0 ;
00306 e->param[1] =p1 ;
00307 return e;
00308 }
00309
00310 static AVEvalExpr * parse_pow(Parser *p, int *sign){
00311 *sign= (*p->s == '+') - (*p->s == '-');
00312 p->s += *sign&1;
00313 return parse_primary(p);
00314 }
00315
00316 static AVEvalExpr * parse_factor(Parser *p){
00317 int sign, sign2;
00318 AVEvalExpr * e = parse_pow(p, &sign);
00319 while(p->s[0]=='^'){
00320 p->s++;
00321 e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
00322 if (e->param[1]) e->param[1]->value *= (sign2|1);
00323 }
00324 if (e) e->value *= (sign|1);
00325 return e;
00326 }
00327
00328 static AVEvalExpr * parse_term(Parser *p){
00329 AVEvalExpr * e = parse_factor(p);
00330 while(p->s[0]=='*' || p->s[0]=='/'){
00331 int c= *p->s++;
00332 e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
00333 }
00334 return e;
00335 }
00336
00337 static AVEvalExpr * parse_subexpr(Parser *p) {
00338 AVEvalExpr * e = parse_term(p);
00339 while(*p->s == '+' || *p->s == '-') {
00340 e= new_eval_expr(e_add, 1, e, parse_term(p));
00341 };
00342
00343 return e;
00344 }
00345
00346 static AVEvalExpr * parse_expr(Parser *p) {
00347 AVEvalExpr * e;
00348
00349 if(p->stack_index <= 0)
00350 return NULL;
00351 p->stack_index--;
00352
00353 e = parse_subexpr(p);
00354
00355 while(*p->s == ';') {
00356 p->s++;
00357 e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
00358 };
00359
00360 p->stack_index++;
00361
00362 return e;
00363 }
00364
00365 static int verify_expr(AVEvalExpr * e) {
00366 if (!e) return 0;
00367 switch (e->type) {
00368 case e_value:
00369 case e_const: return 1;
00370 case e_func0:
00371 case e_func1:
00372 case e_squish:
00373 case e_ld:
00374 case e_gauss: return verify_expr(e->param[0]);
00375 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
00376 }
00377 }
00378
00379 AVEvalExpr * ff_parse(char *s, const char **const_name,
00380 double (**func1)(void *, double), const char **func1_name,
00381 double (**func2)(void *, double, double), char **func2_name,
00382 const char **error){
00383 Parser p;
00384 AVEvalExpr * e;
00385 char w[strlen(s) + 1], * wp = w;
00386
00387 while (*s)
00388 if (!isspace(*s++)) *wp++ = s[-1];
00389 *wp++ = 0;
00390
00391 p.stack_index=100;
00392 p.s= w;
00393 p.const_name = const_name;
00394 p.func1 = func1;
00395 p.func1_name = func1_name;
00396 p.func2 = func2;
00397 p.func2_name = func2_name;
00398 p.error= error;
00399
00400 e = parse_expr(&p);
00401 if (!verify_expr(e)) {
00402 ff_eval_free(e);
00403 return NULL;
00404 }
00405 return e;
00406 }
00407
00408 double ff_parse_eval(AVEvalExpr * e, double *const_value, void *opaque) {
00409 Parser p;
00410
00411 p.const_value= const_value;
00412 p.opaque = opaque;
00413 return eval_expr(&p, e);
00414 }
00415
00416 double ff_eval2(char *s, double *const_value, const char **const_name,
00417 double (**func1)(void *, double), const char **func1_name,
00418 double (**func2)(void *, double, double), char **func2_name,
00419 void *opaque, const char **error){
00420 AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
00421 double d;
00422 if (!e) return NAN;
00423 d = ff_parse_eval(e, const_value, opaque);
00424 ff_eval_free(e);
00425 return d;
00426 }
00427
00428 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
00429 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
00430 double (**func1)(void *, double), const char **func1_name,
00431 double (**func2)(void *, double, double), char **func2_name,
00432 void *opaque){
00433 const char *error=NULL;
00434 double ret;
00435 ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
00436 if (error)
00437 av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
00438 return ret;
00439 }
00440 #endif
00441
00442 #ifdef TEST
00443 #undef printf
00444 static double const_values[]={
00445 M_PI,
00446 M_E,
00447 0
00448 };
00449 static const char *const_names[]={
00450 "PI",
00451 "E",
00452 0
00453 };
00454 int main(void){
00455 int i;
00456 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));
00457 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
00458
00459 for(i=0; i<1050; i++){
00460 START_TIMER
00461 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);
00462 STOP_TIMER("ff_eval")
00463 }
00464 return 0;
00465 }
00466 #endif