2 * simple arithmetic expression evaluator
4 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @file libavcodec/eval.c
26 * simple arithmetic expression evaluator.
28 * see http://joe.hotchkiss.com/programming/eval/eval.html
44 #define M_PI 3.14159265358979323846
47 typedef struct Parser{
50 const double *const_value;
51 const char * const *const_name; // NULL terminated
52 double (**func1)(void *, double a); // NULL terminated
53 const char **func1_name; // NULL terminated
54 double (**func2)(void *, double a, double b); // NULL terminated
55 const char **func2_name; // NULL terminated
62 static const int8_t si_prefixes['z' - 'E' + 1]={
85 double av_strtod(const char *numstr, char **tail) {
88 d = strtod(numstr, &next);
89 /* if parsing succeeded, check for and interpret postfixes */
92 if(*next >= 'E' && *next <= 'z'){
93 int e= si_prefixes[*next - 'E'];
110 /* if requested, fill in tail with the position after the last parsed
117 static int strmatch(const char *s, const char *prefix){
119 for(i=0; prefix[i]; i++){
120 if(prefix[i] != s[i]) return 0;
127 e_value, e_const, e_func0, e_func1, e_func2,
128 e_squish, e_gauss, e_ld,
129 e_mod, e_max, e_min, e_eq, e_gt, e_gte,
130 e_pow, e_mul, e_div, e_add,
131 e_last, e_st, e_while,
133 double value; // is sign in other types
136 double (*func0)(double);
137 double (*func1)(void *, double);
138 double (*func2)(void *, double, double);
140 AVEvalExpr * param[2];
143 static double eval_expr(Parser * p, AVEvalExpr * e) {
145 case e_value: return e->value;
146 case e_const: return e->value * p->const_value[e->a.const_index];
147 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
148 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
149 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
150 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
151 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
152 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
155 while(eval_expr(p, e->param[0]))
156 d=eval_expr(p, e->param[1]);
160 double d = eval_expr(p, e->param[0]);
161 double d2 = eval_expr(p, e->param[1]);
163 case e_mod: return e->value * (d - floor(d/d2)*d2);
164 case e_max: return e->value * (d > d2 ? d : d2);
165 case e_min: return e->value * (d < d2 ? d : d2);
166 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
167 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
168 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
169 case e_pow: return e->value * pow(d, d2);
170 case e_mul: return e->value * (d * d2);
171 case e_div: return e->value * (d / d2);
172 case e_add: return e->value * (d + d2);
173 case e_last:return e->value * d2;
174 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
181 static AVEvalExpr * parse_expr(Parser *p);
183 void ff_eval_free(AVEvalExpr * e) {
185 ff_eval_free(e->param[0]);
186 ff_eval_free(e->param[1]);
190 static AVEvalExpr * parse_primary(Parser *p) {
191 AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr));
196 d->value = av_strtod(p->s, &next);
204 /* named constants */
205 for(i=0; p->const_name && p->const_name[i]; i++){
206 if(strmatch(p->s, p->const_name[i])){
207 p->s+= strlen(p->const_name[i]);
209 d->a.const_index = i;
214 p->s= strchr(p->s, '(');
216 *p->error = "undefined constant or missing (";
222 if (*next == '(') { // special case do-nothing
226 *p->error = "missing )";
233 d->param[0] = parse_expr(p);
236 d->param[1] = parse_expr(p);
239 *p->error = "missing )";
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" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
267 else if( strmatch(next, "lt" ) ) { AVEvalExpr * 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];
280 for(i=0; p->func2_name && p->func2_name[i]; i++){
281 if(strmatch(next, p->func2_name[i])){
282 d->a.func2 = p->func2[i];
288 *p->error = "unknown function";
296 static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){
297 AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr));
305 static AVEvalExpr * parse_pow(Parser *p, int *sign){
306 *sign= (*p->s == '+') - (*p->s == '-');
308 return parse_primary(p);
311 static AVEvalExpr * parse_factor(Parser *p){
313 AVEvalExpr * e = parse_pow(p, &sign);
316 e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
317 if (e->param[1]) e->param[1]->value *= (sign2|1);
319 if (e) e->value *= (sign|1);
323 static AVEvalExpr * parse_term(Parser *p){
324 AVEvalExpr * e = parse_factor(p);
325 while(p->s[0]=='*' || p->s[0]=='/'){
327 e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
332 static AVEvalExpr * parse_subexpr(Parser *p) {
333 AVEvalExpr * e = parse_term(p);
334 while(*p->s == '+' || *p->s == '-') {
335 e= new_eval_expr(e_add, 1, e, parse_term(p));
341 static AVEvalExpr * parse_expr(Parser *p) {
344 if(p->stack_index <= 0) //protect against stack overflows
348 e = parse_subexpr(p);
350 while(*p->s == ';') {
352 e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
360 static int verify_expr(AVEvalExpr * e) {
364 case e_const: return 1;
369 case e_gauss: return verify_expr(e->param[0]);
370 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
374 AVEvalExpr * ff_parse(const char *s, const char * const *const_name,
375 double (**func1)(void *, double), const char **func1_name,
376 double (**func2)(void *, double, double), const char **func2_name,
380 char w[strlen(s) + 1], * wp = w;
383 if (!isspace(*s++)) *wp++ = s[-1];
388 p.const_name = const_name;
390 p.func1_name = func1_name;
392 p.func2_name = func2_name;
396 if (!verify_expr(e)) {
403 double ff_parse_eval(AVEvalExpr * e, const double *const_value, void *opaque) {
406 p.const_value= const_value;
408 return eval_expr(&p, e);
411 double ff_eval2(const char *s, const double *const_value, const char * const *const_name,
412 double (**func1)(void *, double), const char **func1_name,
413 double (**func2)(void *, double, double), const char **func2_name,
414 void *opaque, const char **error){
415 AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
418 d = ff_parse_eval(e, const_value, opaque);
425 static double const_values[]={
430 static const char *const_names[]={
437 printf("%f == 12.7\n", ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
438 printf("%f == 0.931322575\n", ff_eval2("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
440 for(i=0; i<1050; i++){
442 ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL);
443 STOP_TIMER("ff_eval2")