FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
eval.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * simple arithmetic expression evaluator.
25  *
26  * see http://joe.hotchkiss.com/programming/eval/eval.html
27  */
28 
29 #include <float.h>
30 #include "avutil.h"
31 #include "common.h"
32 #include "eval.h"
33 #include "log.h"
34 #include "mathematics.h"
35 
36 typedef struct Parser {
37  const AVClass *class;
39  char *s;
40  const double *const_values;
41  const char * const *const_names; // NULL terminated
42  double (* const *funcs1)(void *, double a); // NULL terminated
43  const char * const *func1_names; // NULL terminated
44  double (* const *funcs2)(void *, double a, double b); // NULL terminated
45  const char * const *func2_names; // NULL terminated
46  void *opaque;
48  void *log_ctx;
49 #define VARS 10
50  double *var;
51 } Parser;
52 
53 static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
54 
55 static const int8_t si_prefixes['z' - 'E' + 1] = {
56  ['y'-'E']= -24,
57  ['z'-'E']= -21,
58  ['a'-'E']= -18,
59  ['f'-'E']= -15,
60  ['p'-'E']= -12,
61  ['n'-'E']= - 9,
62  ['u'-'E']= - 6,
63  ['m'-'E']= - 3,
64  ['c'-'E']= - 2,
65  ['d'-'E']= - 1,
66  ['h'-'E']= 2,
67  ['k'-'E']= 3,
68  ['K'-'E']= 3,
69  ['M'-'E']= 6,
70  ['G'-'E']= 9,
71  ['T'-'E']= 12,
72  ['P'-'E']= 15,
73  ['E'-'E']= 18,
74  ['Z'-'E']= 21,
75  ['Y'-'E']= 24,
76 };
77 
78 static const struct {
79  const char *name;
80  double value;
81 } constants[] = {
82  { "E", M_E },
83  { "PI", M_PI },
84  { "PHI", M_PHI },
85 };
86 
87 double av_strtod(const char *numstr, char **tail)
88 {
89  double d;
90  char *next;
91  if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
92  d = strtoul(numstr, &next, 16);
93  } else
94  d = strtod(numstr, &next);
95  /* if parsing succeeded, check for and interpret postfixes */
96  if (next!=numstr) {
97  if (next[0] == 'd' && next[1] == 'B') {
98  /* treat dB as decibels instead of decibytes */
99  d = pow(10, d / 20);
100  next += 2;
101  } else if (*next >= 'E' && *next <= 'z') {
102  int e= si_prefixes[*next - 'E'];
103  if (e) {
104  if (next[1] == 'i') {
105  d*= pow( 2, e/0.3);
106  next+=2;
107  } else {
108  d*= pow(10, e);
109  next++;
110  }
111  }
112  }
113 
114  if (*next=='B') {
115  d*=8;
116  next++;
117  }
118  }
119  /* if requested, fill in tail with the position after the last parsed
120  character */
121  if (tail)
122  *tail = next;
123  return d;
124 }
125 
126 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
127 
128 static int strmatch(const char *s, const char *prefix)
129 {
130  int i;
131  for (i=0; prefix[i]; i++) {
132  if (prefix[i] != s[i]) return 0;
133  }
134  /* return 1 only if the s identifier is terminated */
135  return !IS_IDENTIFIER_CHAR(s[i]);
136 }
137 
138 struct AVExpr {
139  enum {
147  } type;
148  double value; // is sign in other types
149  union {
151  double (*func0)(double);
152  double (*func1)(void *, double);
153  double (*func2)(void *, double, double);
154  } a;
155  struct AVExpr *param[3];
156  double *var;
157 };
158 
159 static double eval_expr(Parser *p, AVExpr *e)
160 {
161  switch (e->type) {
162  case e_value: return e->value;
163  case e_const: return e->value * p->const_values[e->a.const_index];
164  case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
165  case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
166  case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
167  case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
168  case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
169  case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
170  case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
171  case e_isinf: return e->value * !!isinf(eval_expr(p, e->param[0]));
172  case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
173  case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
174  case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
175  case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
176  case e_not: return e->value * (eval_expr(p, e->param[0]) == 0);
177  case e_if: return e->value * ( eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : 0);
178  case e_ifnot: return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : 0);
179  case e_random:{
180  int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
181  uint64_t r= isnan(p->var[idx]) ? 0 : p->var[idx];
182  r= r*1664525+1013904223;
183  p->var[idx]= r;
184  return e->value * (r * (1.0/UINT64_MAX));
185  }
186  case e_while: {
187  double d = NAN;
188  while (eval_expr(p, e->param[0]))
189  d=eval_expr(p, e->param[1]);
190  return d;
191  }
192  case e_taylor: {
193  double t = 1, d = 0, v;
194  double x = eval_expr(p, e->param[1]);
195  int id = e->param[2] ? av_clip(eval_expr(p, e->param[2]), 0, VARS-1) : 0;
196  int i;
197  double var0 = p->var[id];
198  for(i=0; i<1000; i++) {
199  double ld = d;
200  p->var[id] = i;
201  v = eval_expr(p, e->param[0]);
202  d += t*v;
203  if(ld==d && v)
204  break;
205  t *= x / (i+1);
206  }
207  p->var[id] = var0;
208  return d;
209  }
210  case e_root: {
211  int i, j;
212  double low = -1, high = -1, v, low_v = -DBL_MAX, high_v = DBL_MAX;
213  double var0 = p->var[0];
214  double x_max = eval_expr(p, e->param[1]);
215  for(i=-1; i<1024; i++) {
216  if(i<255) {
217  p->var[0] = av_reverse[i&255]*x_max/255;
218  } else {
219  p->var[0] = x_max*pow(0.9, i-255);
220  if (i&1) p->var[0] *= -1;
221  if (i&2) p->var[0] += low;
222  else p->var[0] += high;
223  }
224  v = eval_expr(p, e->param[0]);
225  if (v<=0 && v>low_v) {
226  low = p->var[0];
227  low_v = v;
228  }
229  if (v>=0 && v<high_v) {
230  high = p->var[0];
231  high_v = v;
232  }
233  if (low>=0 && high>=0){
234  for (j=0; j<1000; j++) {
235  p->var[0] = (low+high)*0.5;
236  if (low == p->var[0] || high == p->var[0])
237  break;
238  v = eval_expr(p, e->param[0]);
239  if (v<=0) low = p->var[0];
240  if (v>=0) high= p->var[0];
241  if (isnan(v)) {
242  low = high = v;
243  break;
244  }
245  }
246  break;
247  }
248  }
249  p->var[0] = var0;
250  return -low_v<high_v ? low : high;
251  }
252  default: {
253  double d = eval_expr(p, e->param[0]);
254  double d2 = eval_expr(p, e->param[1]);
255  switch (e->type) {
256  case e_mod: return e->value * (d - floor((!CONFIG_FTRAPV || d2) ? d / d2 : d * INFINITY) * d2);
257  case e_gcd: return e->value * av_gcd(d,d2);
258  case e_max: return e->value * (d > d2 ? d : d2);
259  case e_min: return e->value * (d < d2 ? d : d2);
260  case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
261  case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
262  case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
263  case e_pow: return e->value * pow(d, d2);
264  case e_mul: return e->value * (d * d2);
265  case e_div: return e->value * ((!CONFIG_FTRAPV || d2 ) ? (d / d2) : d * INFINITY);
266  case e_add: return e->value * (d + d2);
267  case e_last:return e->value * d2;
268  case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
269  case e_hypot:return e->value * (sqrt(d*d + d2*d2));
270  }
271  }
272  }
273  return NAN;
274 }
275 
276 static int parse_expr(AVExpr **e, Parser *p);
277 
279 {
280  if (!e) return;
281  av_expr_free(e->param[0]);
282  av_expr_free(e->param[1]);
283  av_expr_free(e->param[2]);
284  av_freep(&e->var);
285  av_freep(&e);
286 }
287 
288 static int parse_primary(AVExpr **e, Parser *p)
289 {
290  AVExpr *d = av_mallocz(sizeof(AVExpr));
291  char *next = p->s, *s0 = p->s;
292  int ret, i;
293 
294  if (!d)
295  return AVERROR(ENOMEM);
296 
297  /* number */
298  d->value = av_strtod(p->s, &next);
299  if (next != p->s) {
300  d->type = e_value;
301  p->s= next;
302  *e = d;
303  return 0;
304  }
305  d->value = 1;
306 
307  /* named constants */
308  for (i=0; p->const_names && p->const_names[i]; i++) {
309  if (strmatch(p->s, p->const_names[i])) {
310  p->s+= strlen(p->const_names[i]);
311  d->type = e_const;
312  d->a.const_index = i;
313  *e = d;
314  return 0;
315  }
316  }
317  for (i = 0; i < FF_ARRAY_ELEMS(constants); i++) {
318  if (strmatch(p->s, constants[i].name)) {
319  p->s += strlen(constants[i].name);
320  d->type = e_value;
321  d->value = constants[i].value;
322  *e = d;
323  return 0;
324  }
325  }
326 
327  p->s= strchr(p->s, '(');
328  if (p->s==NULL) {
329  av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
330  p->s= next;
331  av_expr_free(d);
332  return AVERROR(EINVAL);
333  }
334  p->s++; // "("
335  if (*next == '(') { // special case do-nothing
336  av_freep(&d);
337  if ((ret = parse_expr(&d, p)) < 0)
338  return ret;
339  if (p->s[0] != ')') {
340  av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
341  av_expr_free(d);
342  return AVERROR(EINVAL);
343  }
344  p->s++; // ")"
345  *e = d;
346  return 0;
347  }
348  if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
349  av_expr_free(d);
350  return ret;
351  }
352  if (p->s[0]== ',') {
353  p->s++; // ","
354  parse_expr(&d->param[1], p);
355  }
356  if (p->s[0]== ',') {
357  p->s++; // ","
358  parse_expr(&d->param[2], p);
359  }
360  if (p->s[0] != ')') {
361  av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
362  av_expr_free(d);
363  return AVERROR(EINVAL);
364  }
365  p->s++; // ")"
366 
367  d->type = e_func0;
368  if (strmatch(next, "sinh" )) d->a.func0 = sinh;
369  else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
370  else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
371  else if (strmatch(next, "sin" )) d->a.func0 = sin;
372  else if (strmatch(next, "cos" )) d->a.func0 = cos;
373  else if (strmatch(next, "tan" )) d->a.func0 = tan;
374  else if (strmatch(next, "atan" )) d->a.func0 = atan;
375  else if (strmatch(next, "asin" )) d->a.func0 = asin;
376  else if (strmatch(next, "acos" )) d->a.func0 = acos;
377  else if (strmatch(next, "exp" )) d->a.func0 = exp;
378  else if (strmatch(next, "log" )) d->a.func0 = log;
379  else if (strmatch(next, "abs" )) d->a.func0 = fabs;
380  else if (strmatch(next, "squish")) d->type = e_squish;
381  else if (strmatch(next, "gauss" )) d->type = e_gauss;
382  else if (strmatch(next, "mod" )) d->type = e_mod;
383  else if (strmatch(next, "max" )) d->type = e_max;
384  else if (strmatch(next, "min" )) d->type = e_min;
385  else if (strmatch(next, "eq" )) d->type = e_eq;
386  else if (strmatch(next, "gte" )) d->type = e_gte;
387  else if (strmatch(next, "gt" )) d->type = e_gt;
388  else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
389  else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
390  else if (strmatch(next, "ld" )) d->type = e_ld;
391  else if (strmatch(next, "isnan" )) d->type = e_isnan;
392  else if (strmatch(next, "isinf" )) d->type = e_isinf;
393  else if (strmatch(next, "st" )) d->type = e_st;
394  else if (strmatch(next, "while" )) d->type = e_while;
395  else if (strmatch(next, "taylor")) d->type = e_taylor;
396  else if (strmatch(next, "root" )) d->type = e_root;
397  else if (strmatch(next, "floor" )) d->type = e_floor;
398  else if (strmatch(next, "ceil" )) d->type = e_ceil;
399  else if (strmatch(next, "trunc" )) d->type = e_trunc;
400  else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
401  else if (strmatch(next, "not" )) d->type = e_not;
402  else if (strmatch(next, "pow" )) d->type = e_pow;
403  else if (strmatch(next, "random")) d->type = e_random;
404  else if (strmatch(next, "hypot" )) d->type = e_hypot;
405  else if (strmatch(next, "gcd" )) d->type = e_gcd;
406  else if (strmatch(next, "if" )) d->type = e_if;
407  else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
408  else {
409  for (i=0; p->func1_names && p->func1_names[i]; i++) {
410  if (strmatch(next, p->func1_names[i])) {
411  d->a.func1 = p->funcs1[i];
412  d->type = e_func1;
413  *e = d;
414  return 0;
415  }
416  }
417 
418  for (i=0; p->func2_names && p->func2_names[i]; i++) {
419  if (strmatch(next, p->func2_names[i])) {
420  d->a.func2 = p->funcs2[i];
421  d->type = e_func2;
422  *e = d;
423  return 0;
424  }
425  }
426 
427  av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
428  av_expr_free(d);
429  return AVERROR(EINVAL);
430  }
431 
432  *e = d;
433  return 0;
434 }
435 
436 static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
437 {
438  AVExpr *e = av_mallocz(sizeof(AVExpr));
439  if (!e)
440  return NULL;
441  e->type =type ;
442  e->value =value ;
443  e->param[0] =p0 ;
444  e->param[1] =p1 ;
445  return e;
446 }
447 
448 static int parse_pow(AVExpr **e, Parser *p, int *sign)
449 {
450  *sign= (*p->s == '+') - (*p->s == '-');
451  p->s += *sign&1;
452  return parse_primary(e, p);
453 }
454 
455 static int parse_dB(AVExpr **e, Parser *p, int *sign)
456 {
457  /* do not filter out the negative sign when parsing a dB value.
458  for example, -3dB is not the same as -(3dB) */
459  if (*p->s == '-') {
460  char *next;
461  strtod(p->s, &next);
462  if (next != p->s && next[0] == 'd' && next[1] == 'B') {
463  *sign = 0;
464  return parse_primary(e, p);
465  }
466  }
467  return parse_pow(e, p, sign);
468 }
469 
470 static int parse_factor(AVExpr **e, Parser *p)
471 {
472  int sign, sign2, ret;
473  AVExpr *e0, *e1, *e2;
474  if ((ret = parse_dB(&e0, p, &sign)) < 0)
475  return ret;
476  while(p->s[0]=='^'){
477  e1 = e0;
478  p->s++;
479  if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
480  av_expr_free(e1);
481  return ret;
482  }
483  e0 = new_eval_expr(e_pow, 1, e1, e2);
484  if (!e0) {
485  av_expr_free(e1);
486  av_expr_free(e2);
487  return AVERROR(ENOMEM);
488  }
489  if (e0->param[1]) e0->param[1]->value *= (sign2|1);
490  }
491  if (e0) e0->value *= (sign|1);
492 
493  *e = e0;
494  return 0;
495 }
496 
497 static int parse_term(AVExpr **e, Parser *p)
498 {
499  int ret;
500  AVExpr *e0, *e1, *e2;
501  if ((ret = parse_factor(&e0, p)) < 0)
502  return ret;
503  while (p->s[0]=='*' || p->s[0]=='/') {
504  int c= *p->s++;
505  e1 = e0;
506  if ((ret = parse_factor(&e2, p)) < 0) {
507  av_expr_free(e1);
508  return ret;
509  }
510  e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
511  if (!e0) {
512  av_expr_free(e1);
513  av_expr_free(e2);
514  return AVERROR(ENOMEM);
515  }
516  }
517  *e = e0;
518  return 0;
519 }
520 
521 static int parse_subexpr(AVExpr **e, Parser *p)
522 {
523  int ret;
524  AVExpr *e0, *e1, *e2;
525  if ((ret = parse_term(&e0, p)) < 0)
526  return ret;
527  while (*p->s == '+' || *p->s == '-') {
528  e1 = e0;
529  if ((ret = parse_term(&e2, p)) < 0) {
530  av_expr_free(e1);
531  return ret;
532  }
533  e0 = new_eval_expr(e_add, 1, e1, e2);
534  if (!e0) {
535  av_expr_free(e1);
536  av_expr_free(e2);
537  return AVERROR(ENOMEM);
538  }
539  };
540 
541  *e = e0;
542  return 0;
543 }
544 
545 static int parse_expr(AVExpr **e, Parser *p)
546 {
547  int ret;
548  AVExpr *e0, *e1, *e2;
549  if (p->stack_index <= 0) //protect against stack overflows
550  return AVERROR(EINVAL);
551  p->stack_index--;
552 
553  if ((ret = parse_subexpr(&e0, p)) < 0)
554  return ret;
555  while (*p->s == ';') {
556  p->s++;
557  e1 = e0;
558  if ((ret = parse_subexpr(&e2, p)) < 0) {
559  av_expr_free(e1);
560  return ret;
561  }
562  e0 = new_eval_expr(e_last, 1, e1, e2);
563  if (!e0) {
564  av_expr_free(e1);
565  av_expr_free(e2);
566  return AVERROR(ENOMEM);
567  }
568  };
569 
570  p->stack_index++;
571  *e = e0;
572  return 0;
573 }
574 
575 static int verify_expr(AVExpr *e)
576 {
577  if (!e) return 0;
578  switch (e->type) {
579  case e_value:
580  case e_const: return 1;
581  case e_func0:
582  case e_func1:
583  case e_squish:
584  case e_ld:
585  case e_gauss:
586  case e_isnan:
587  case e_isinf:
588  case e_floor:
589  case e_ceil:
590  case e_trunc:
591  case e_sqrt:
592  case e_not:
593  case e_random:
594  return verify_expr(e->param[0]) && !e->param[1];
595  case e_taylor:
596  return verify_expr(e->param[0]) && verify_expr(e->param[1])
597  && (!e->param[2] || verify_expr(e->param[2]));
598  default: return verify_expr(e->param[0]) && verify_expr(e->param[1]) && !e->param[2];
599  }
600 }
601 
602 int av_expr_parse(AVExpr **expr, const char *s,
603  const char * const *const_names,
604  const char * const *func1_names, double (* const *funcs1)(void *, double),
605  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
606  int log_offset, void *log_ctx)
607 {
608  Parser p = { 0 };
609  AVExpr *e = NULL;
610  char *w = av_malloc(strlen(s) + 1);
611  char *wp = w;
612  const char *s0 = s;
613  int ret = 0;
614 
615  if (!w)
616  return AVERROR(ENOMEM);
617 
618  while (*s)
619  if (!isspace(*s++)) *wp++ = s[-1];
620  *wp++ = 0;
621 
622  p.class = &class;
623  p.stack_index=100;
624  p.s= w;
626  p.funcs1 = funcs1;
627  p.func1_names = func1_names;
628  p.funcs2 = funcs2;
629  p.func2_names = func2_names;
630  p.log_offset = log_offset;
631  p.log_ctx = log_ctx;
632 
633  if ((ret = parse_expr(&e, &p)) < 0)
634  goto end;
635  if (*p.s) {
636  av_expr_free(e);
637  av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
638  ret = AVERROR(EINVAL);
639  goto end;
640  }
641  if (!verify_expr(e)) {
642  av_expr_free(e);
643  ret = AVERROR(EINVAL);
644  goto end;
645  }
646  e->var= av_mallocz(sizeof(double) *VARS);
647  *expr = e;
648 end:
649  av_free(w);
650  return ret;
651 }
652 
653 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
654 {
655  Parser p = { 0 };
656  p.var= e->var;
657 
659  p.opaque = opaque;
660  return eval_expr(&p, e);
661 }
662 
663 int av_expr_parse_and_eval(double *d, const char *s,
664  const char * const *const_names, const double *const_values,
665  const char * const *func1_names, double (* const *funcs1)(void *, double),
666  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
667  void *opaque, int log_offset, void *log_ctx)
668 {
669  AVExpr *e = NULL;
670  int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
671 
672  if (ret < 0) {
673  *d = NAN;
674  return ret;
675  }
676  *d = av_expr_eval(e, const_values, opaque);
677  av_expr_free(e);
678  return isnan(*d) ? AVERROR(EINVAL) : 0;
679 }
680 
681 #ifdef TEST
682 #include <string.h>
683 
684 static const double const_values[] = {
685  M_PI,
686  M_E,
687  0
688 };
689 
690 static const char *const const_names[] = {
691  "PI",
692  "E",
693  0
694 };
695 
696 int main(int argc, char **argv)
697 {
698  int i;
699  double d;
700  const char *const *expr;
701  static const char *const exprs[] = {
702  "",
703  "1;2",
704  "-20",
705  "-PI",
706  "+PI",
707  "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
708  "80G/80Gi",
709  "1k",
710  "1Gi",
711  "1gi",
712  "1GiFoo",
713  "1k+1k",
714  "1Gi*3foo",
715  "foo",
716  "foo(",
717  "foo()",
718  "foo)",
719  "sin",
720  "sin(",
721  "sin()",
722  "sin)",
723  "sin 10",
724  "sin(1,2,3)",
725  "sin(1 )",
726  "1",
727  "1foo",
728  "bar + PI + E + 100f*2 + foo",
729  "13k + 12f - foo(1, 2)",
730  "1gi",
731  "1Gi",
732  "st(0, 123)",
733  "st(1, 123); ld(1)",
734  "lte(0, 1)",
735  "lte(1, 1)",
736  "lte(1, 0)",
737  "lt(0, 1)",
738  "lt(1, 1)",
739  "gt(1, 0)",
740  "gt(2, 7)",
741  "gte(122, 122)",
742  /* compute 1+2+...+N */
743  "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
744  /* compute Fib(N) */
745  "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
746  "while(0, 10)",
747  "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
748  "isnan(1)",
749  "isnan(NAN)",
750  "isnan(INF)",
751  "isinf(1)",
752  "isinf(NAN)",
753  "isinf(INF)",
754  "floor(NAN)",
755  "floor(123.123)",
756  "floor(-123.123)",
757  "trunc(123.123)",
758  "trunc(-123.123)",
759  "ceil(123.123)",
760  "ceil(-123.123)",
761  "sqrt(1764)",
762  "isnan(sqrt(-1))",
763  "not(1)",
764  "not(NAN)",
765  "not(0)",
766  "6.0206dB",
767  "-3.0103dB",
768  "pow(0,1.23)",
769  "pow(PI,1.23)",
770  "PI^1.23",
771  "pow(-1,1.23)",
772  "if(1, 2)",
773  "ifnot(0, 23)",
774  "ifnot(1, NaN) + if(0, 1)",
775  "taylor(1, 1)",
776  "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
777  "root(sin(ld(0))-1, 2)",
778  "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)",
779  "7000000B*random(0)",
780  "squish(2)",
781  "gauss(0.1)",
782  "hypot(4,3)",
783  "gcd(30,55)*min(9,1)",
784  NULL
785  };
786 
787  for (expr = exprs; *expr; expr++) {
788  printf("Evaluating '%s'\n", *expr);
789  av_expr_parse_and_eval(&d, *expr,
790  const_names, const_values,
791  NULL, NULL, NULL, NULL, NULL, 0, NULL);
792  if (isnan(d))
793  printf("'%s' -> nan\n\n", *expr);
794  else
795  printf("'%s' -> %f\n\n", *expr, d);
796  }
797 
798  av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
799  const_names, const_values,
800  NULL, NULL, NULL, NULL, NULL, 0, NULL);
801  printf("%f == 12.7\n", d);
802  av_expr_parse_and_eval(&d, "80G/80Gi",
803  const_names, const_values,
804  NULL, NULL, NULL, NULL, NULL, 0, NULL);
805  printf("%f == 0.931322575\n", d);
806 
807  if (argc > 1 && !strcmp(argv[1], "-t")) {
808  for (i = 0; i < 1050; i++) {
809  START_TIMER;
810  av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
811  const_names, const_values,
812  NULL, NULL, NULL, NULL, NULL, 0, NULL);
813  STOP_TIMER("av_expr_parse_and_eval");
814  }
815  }
816 
817  return 0;
818 }
819 #endif