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