comparison eval.c @ 11802:5880d90f2b99 libavcodec

Cosmetics: rename ff_parse_expr() and ff_parse_and_eval_expr() parameters: const_name -> const_names const_value -> const_values func[12]_name -> func[12]_names func[12] -> funcs[12] All these parameters contain a list of values, using plural names for them help understanding a little.
author stefano
date Tue, 01 Jun 2010 08:07:12 +0000
parents 026edf66e3a9
children f443ef5ec4e2
comparison
equal deleted inserted replaced
11801:026edf66e3a9 11802:5880d90f2b99
31 31
32 typedef struct Parser{ 32 typedef struct Parser{
33 const AVClass *class; 33 const AVClass *class;
34 int stack_index; 34 int stack_index;
35 char *s; 35 char *s;
36 const double *const_value; 36 const double *const_values;
37 const char * const *const_name; // NULL terminated 37 const char * const *const_names; // NULL terminated
38 double (* const *func1)(void *, double a); // NULL terminated 38 double (* const *funcs1)(void *, double a); // NULL terminated
39 const char * const *func1_name; // NULL terminated 39 const char * const *func1_names; // NULL terminated
40 double (* const *func2)(void *, double a, double b); // NULL terminated 40 double (* const *funcs2)(void *, double a, double b); // NULL terminated
41 const char * const *func2_name; // NULL terminated 41 const char * const *func2_names; // NULL terminated
42 void *opaque; 42 void *opaque;
43 int log_offset; 43 int log_offset;
44 void *log_ctx; 44 void *log_ctx;
45 #define VARS 10 45 #define VARS 10
46 double var[VARS]; 46 double var[VARS];
130 }; 130 };
131 131
132 static double eval_expr(Parser * p, AVExpr * e) { 132 static double eval_expr(Parser * p, AVExpr * e) {
133 switch (e->type) { 133 switch (e->type) {
134 case e_value: return e->value; 134 case e_value: return e->value;
135 case e_const: return e->value * p->const_value[e->a.const_index]; 135 case e_const: return e->value * p->const_values[e->a.const_index];
136 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0])); 136 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
137 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0])); 137 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
138 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1])); 138 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
139 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0]))); 139 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
140 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); } 140 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
194 return 0; 194 return 0;
195 } 195 }
196 d->value = 1; 196 d->value = 1;
197 197
198 /* named constants */ 198 /* named constants */
199 for(i=0; p->const_name && p->const_name[i]; i++){ 199 for(i=0; p->const_names && p->const_names[i]; i++){
200 if(strmatch(p->s, p->const_name[i])){ 200 if(strmatch(p->s, p->const_names[i])){
201 p->s+= strlen(p->const_name[i]); 201 p->s+= strlen(p->const_names[i]);
202 d->type = e_const; 202 d->type = e_const;
203 d->a.const_index = i; 203 d->a.const_index = i;
204 *e = d; 204 *e = d;
205 return 0; 205 return 0;
206 } 206 }
267 else if( strmatch(next, "lt" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; } 267 else if( strmatch(next, "lt" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
268 else if( strmatch(next, "ld" ) ) d->type = e_ld; 268 else if( strmatch(next, "ld" ) ) d->type = e_ld;
269 else if( strmatch(next, "st" ) ) d->type = e_st; 269 else if( strmatch(next, "st" ) ) d->type = e_st;
270 else if( strmatch(next, "while" ) ) d->type = e_while; 270 else if( strmatch(next, "while" ) ) d->type = e_while;
271 else { 271 else {
272 for(i=0; p->func1_name && p->func1_name[i]; i++){ 272 for(i=0; p->func1_names && p->func1_names[i]; i++){
273 if(strmatch(next, p->func1_name[i])){ 273 if(strmatch(next, p->func1_names[i])){
274 d->a.func1 = p->func1[i]; 274 d->a.func1 = p->funcs1[i];
275 d->type = e_func1; 275 d->type = e_func1;
276 *e = d; 276 *e = d;
277 return 0; 277 return 0;
278 } 278 }
279 } 279 }
280 280
281 for(i=0; p->func2_name && p->func2_name[i]; i++){ 281 for(i=0; p->func2_names && p->func2_names[i]; i++){
282 if(strmatch(next, p->func2_name[i])){ 282 if(strmatch(next, p->func2_names[i])){
283 d->a.func2 = p->func2[i]; 283 d->a.func2 = p->funcs2[i];
284 d->type = e_func2; 284 d->type = e_func2;
285 *e = d; 285 *e = d;
286 return 0; 286 return 0;
287 } 287 }
288 } 288 }
432 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]); 432 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
433 } 433 }
434 } 434 }
435 435
436 int ff_parse_expr(AVExpr **expr, const char *s, 436 int ff_parse_expr(AVExpr **expr, const char *s,
437 const char * const *const_name, 437 const char * const *const_names,
438 const char * const *func1_name, double (* const *func1)(void *, double), 438 const char * const *func1_names, double (* const *funcs1)(void *, double),
439 const char * const *func2_name, double (* const *func2)(void *, double, double), 439 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
440 int log_offset, void *log_ctx) 440 int log_offset, void *log_ctx)
441 { 441 {
442 Parser p; 442 Parser p;
443 AVExpr *e = NULL; 443 AVExpr *e = NULL;
444 char *w = av_malloc(strlen(s) + 1); 444 char *w = av_malloc(strlen(s) + 1);
453 *wp++ = 0; 453 *wp++ = 0;
454 454
455 p.class = &class; 455 p.class = &class;
456 p.stack_index=100; 456 p.stack_index=100;
457 p.s= w; 457 p.s= w;
458 p.const_name = const_name; 458 p.const_names = const_names;
459 p.func1 = func1; 459 p.funcs1 = funcs1;
460 p.func1_name = func1_name; 460 p.func1_names = func1_names;
461 p.func2 = func2; 461 p.funcs2 = funcs2;
462 p.func2_name = func2_name; 462 p.func2_names = func2_names;
463 p.log_offset = log_offset; 463 p.log_offset = log_offset;
464 p.log_ctx = log_ctx; 464 p.log_ctx = log_ctx;
465 465
466 if ((ret = parse_expr(&e, &p)) < 0) 466 if ((ret = parse_expr(&e, &p)) < 0)
467 goto end; 467 goto end;
474 end: 474 end:
475 av_free(w); 475 av_free(w);
476 return ret; 476 return ret;
477 } 477 }
478 478
479 double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) { 479 double ff_eval_expr(AVExpr *e, const double *const_values, void *opaque) {
480 Parser p; 480 Parser p;
481 481
482 p.const_value= const_value; 482 p.const_values = const_values;
483 p.opaque = opaque; 483 p.opaque = opaque;
484 return eval_expr(&p, e); 484 return eval_expr(&p, e);
485 } 485 }
486 486
487 int ff_parse_and_eval_expr(double *d, const char *s, 487 int ff_parse_and_eval_expr(double *d, const char *s,
488 const char * const *const_name, const double *const_value, 488 const char * const *const_names, const double *const_values,
489 const char * const *func1_name, double (* const *func1)(void *, double), 489 const char * const *func1_names, double (* const *funcs1)(void *, double),
490 const char * const *func2_name, double (* const *func2)(void *, double, double), 490 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
491 void *opaque, int log_offset, void *log_ctx) 491 void *opaque, int log_offset, void *log_ctx)
492 { 492 {
493 AVExpr *e = NULL; 493 AVExpr *e = NULL;
494 int ret = ff_parse_expr(&e, s, const_name, func1_name, func1, func2_name, func2, log_offset, log_ctx); 494 int ret = ff_parse_expr(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
495 495
496 if (ret < 0) { 496 if (ret < 0) {
497 *d = NAN; 497 *d = NAN;
498 return ret; 498 return ret;
499 } 499 }
500 *d = ff_eval_expr(e, const_value, opaque); 500 *d = ff_eval_expr(e, const_values, opaque);
501 ff_free_expr(e); 501 ff_free_expr(e);
502 return isnan(*d) ? AVERROR(EINVAL) : 0; 502 return isnan(*d) ? AVERROR(EINVAL) : 0;
503 } 503 }
504 504
505 #ifdef TEST 505 #ifdef TEST