2 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * simple arithmetic expression evaluator.
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
29 #include "libavutil/avutil.h"
32 typedef struct Parser{
36 const double *const_value;
37 const char * const *const_name; // NULL terminated
38 double (* const *func1)(void *, double a); // NULL terminated
39 const char * const *func1_name; // NULL terminated
40 double (* const *func2)(void *, double a, double b); // NULL terminated
41 const char * const *func2_name; // NULL terminated
49 static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
51 static const int8_t si_prefixes['z' - 'E' + 1]={
74 double av_strtod(const char *numstr, char **tail) {
77 d = strtod(numstr, &next);
78 /* if parsing succeeded, check for and interpret postfixes */
81 if(*next >= 'E' && *next <= 'z'){
82 int e= si_prefixes[*next - 'E'];
99 /* if requested, fill in tail with the position after the last parsed
106 static int strmatch(const char *s, const char *prefix){
108 for(i=0; prefix[i]; i++){
109 if(prefix[i] != s[i]) return 0;
116 e_value, e_const, e_func0, e_func1, e_func2,
117 e_squish, e_gauss, e_ld,
118 e_mod, e_max, e_min, e_eq, e_gt, e_gte,
119 e_pow, e_mul, e_div, e_add,
120 e_last, e_st, e_while,
122 double value; // is sign in other types
125 double (*func0)(double);
126 double (*func1)(void *, double);
127 double (*func2)(void *, double, double);
129 struct AVExpr *param[2];
132 static double eval_expr(Parser * p, AVExpr * e) {
134 case e_value: return e->value;
135 case e_const: return e->value * p->const_value[e->a.const_index];
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]));
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])));
140 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
141 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
144 while(eval_expr(p, e->param[0]))
145 d=eval_expr(p, e->param[1]);
149 double d = eval_expr(p, e->param[0]);
150 double d2 = eval_expr(p, e->param[1]);
152 case e_mod: return e->value * (d - floor(d/d2)*d2);
153 case e_max: return e->value * (d > d2 ? d : d2);
154 case e_min: return e->value * (d < d2 ? d : d2);
155 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
156 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
157 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
158 case e_pow: return e->value * pow(d, d2);
159 case e_mul: return e->value * (d * d2);
160 case e_div: return e->value * (d / d2);
161 case e_add: return e->value * (d + d2);
162 case e_last:return e->value * d2;
163 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
170 static int parse_expr(AVExpr **e, Parser *p);
172 void ff_free_expr(AVExpr * e) {
174 ff_free_expr(e->param[0]);
175 ff_free_expr(e->param[1]);
179 static int parse_primary(AVExpr **e, Parser *p)
181 AVExpr * d = av_mallocz(sizeof(AVExpr));
186 return AVERROR(ENOMEM);
189 d->value = av_strtod(p->s, &next);
198 /* named constants */
199 for(i=0; p->const_name && p->const_name[i]; i++){
200 if(strmatch(p->s, p->const_name[i])){
201 p->s+= strlen(p->const_name[i]);
203 d->a.const_index = i;
209 p->s= strchr(p->s, '(');
211 av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n");
214 return AVERROR(EINVAL);
217 if (*next == '(') { // special case do-nothing
219 if ((ret = parse_expr(&d, p)) < 0)
222 av_log(p, AV_LOG_ERROR, "missing )\n");
224 return AVERROR(EINVAL);
230 if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
236 parse_expr(&d->param[1], p);
239 av_log(p, AV_LOG_ERROR, "missing )\n");
241 return AVERROR(EINVAL);
246 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
247 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
248 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
249 else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
250 else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
251 else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
252 else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
253 else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
254 else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
255 else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
256 else if( strmatch(next, "log" ) ) d->a.func0 = log;
257 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
258 else if( strmatch(next, "squish") ) d->type = e_squish;
259 else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
260 else if( strmatch(next, "mod" ) ) d->type = e_mod;
261 else if( strmatch(next, "max" ) ) d->type = e_max;
262 else if( strmatch(next, "min" ) ) d->type = e_min;
263 else if( strmatch(next, "eq" ) ) d->type = e_eq;
264 else if( strmatch(next, "gte" ) ) d->type = e_gte;
265 else if( strmatch(next, "gt" ) ) d->type = e_gt;
266 else if( strmatch(next, "lte" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
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;
269 else if( strmatch(next, "st" ) ) d->type = e_st;
270 else if( strmatch(next, "while" ) ) d->type = e_while;
272 for(i=0; p->func1_name && p->func1_name[i]; i++){
273 if(strmatch(next, p->func1_name[i])){
274 d->a.func1 = p->func1[i];
281 for(i=0; p->func2_name && p->func2_name[i]; i++){
282 if(strmatch(next, p->func2_name[i])){
283 d->a.func2 = p->func2[i];
290 av_log(p, AV_LOG_ERROR, "unknown function\n");
292 return AVERROR(EINVAL);
299 static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1){
300 AVExpr * e = av_mallocz(sizeof(AVExpr));
310 static int parse_pow(AVExpr **e, Parser *p, int *sign)
312 *sign= (*p->s == '+') - (*p->s == '-');
314 return parse_primary(e, p);
317 static int parse_factor(AVExpr **e, Parser *p)
319 int sign, sign2, ret;
320 AVExpr *e0, *e1, *e2;
321 if ((ret = parse_pow(&e0, p, &sign)) < 0)
326 if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
330 e0 = new_eval_expr(e_pow, 1, e1, e2);
334 return AVERROR(ENOMEM);
336 if (e0->param[1]) e0->param[1]->value *= (sign2|1);
338 if (e0) e0->value *= (sign|1);
344 static int parse_term(AVExpr **e, Parser *p)
347 AVExpr *e0, *e1, *e2;
348 if ((ret = parse_factor(&e0, p)) < 0)
350 while(p->s[0]=='*' || p->s[0]=='/'){
353 if ((ret = parse_factor(&e2, p)) < 0) {
357 e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
361 return AVERROR(ENOMEM);
368 static int parse_subexpr(AVExpr **e, Parser *p)
371 AVExpr *e0, *e1, *e2;
372 if ((ret = parse_term(&e0, p)) < 0)
374 while(*p->s == '+' || *p->s == '-') {
376 if ((ret = parse_term(&e2, p)) < 0) {
380 e0 = new_eval_expr(e_add, 1, e1, e2);
384 return AVERROR(ENOMEM);
392 static int parse_expr(AVExpr **e, Parser *p)
395 AVExpr *e0, *e1, *e2;
396 if(p->stack_index <= 0) //protect against stack overflows
397 return AVERROR(EINVAL);
400 if ((ret = parse_subexpr(&e0, p)) < 0)
402 while(*p->s == ';') {
404 if ((ret = parse_subexpr(&e2, p)) < 0) {
409 e0 = new_eval_expr(e_last, 1, e1, e2);
413 return AVERROR(ENOMEM);
422 static int verify_expr(AVExpr * e) {
426 case e_const: return 1;
431 case e_gauss: return verify_expr(e->param[0]);
432 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
436 int ff_parse_expr(AVExpr **expr, const char *s,
437 const char * const *const_name,
438 const char * const *func1_name, double (* const *func1)(void *, double),
439 const char * const *func2_name, double (* const *func2)(void *, double, double),
440 int log_offset, void *log_ctx)
444 char *w = av_malloc(strlen(s) + 1);
449 return AVERROR(ENOMEM);
452 if (!isspace(*s++)) *wp++ = s[-1];
458 p.const_name = const_name;
460 p.func1_name = func1_name;
462 p.func2_name = func2_name;
463 p.log_offset = log_offset;
466 if ((ret = parse_expr(&e, &p)) < 0)
468 if (!verify_expr(e)) {
470 ret = AVERROR(EINVAL);
479 double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) {
482 p.const_value= const_value;
484 return eval_expr(&p, e);
487 int ff_parse_and_eval_expr(double *d, const char *s,
488 const char * const *const_name, const double *const_value,
489 const char * const *func1_name, double (* const *func1)(void *, double),
490 const char * const *func2_name, double (* const *func2)(void *, double, double),
491 void *opaque, int log_offset, void *log_ctx)
494 int ret = ff_parse_expr(&e, s, const_name, func1_name, func1, func2_name, func2, log_offset, log_ctx);
500 *d = ff_eval_expr(e, const_value, opaque);
502 return isnan(*d) ? AVERROR(EINVAL) : 0;
507 static double const_values[]={
512 static const char *const_names[]={
520 ff_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
521 printf("%f == 12.7\n", d);
522 ff_parse_and_eval_expr(&d, "80G/80Gi", const_names, const_values, NULL, NULL, NULL, NULL, NULL, NULL);
523 printf("%f == 0.931322575\n", d);
525 for(i=0; i<1050; i++){
527 ff_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
528 STOP_TIMER("ff_parse_and_eval_expr")