Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 #include "attributes.h"
30 #include "avutil.h"
31 #include "common.h"
32 #include "eval.h"
33 #include "log.h"
34 #include "mathematics.h"
35 #include "avstring.h"
36 #include "timer.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[VARS];
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 double av_strtod(const char *numstr, char **tail)
81 {
82  double d;
83  char *next;
84  d = strtod(numstr, &next);
85  /* if parsing succeeded, check for and interpret postfixes */
86  if (next!=numstr) {
87  if (next[0] == 'd' && next[1] == 'B') {
88  /* treat dB as decibels instead of decibytes */
89  d = pow(10, d / 20);
90  next += 2;
91  } else if (*next >= 'E' && *next <= 'z') {
92  int e= si_prefixes[*next - 'E'];
93  if (e) {
94  if (next[1] == 'i') {
95  d*= pow( 2, e/0.3);
96  next+=2;
97  } else {
98  d*= pow(10, e);
99  next++;
100  }
101  }
102  }
103 
104  if (*next=='B') {
105  d*=8;
106  next++;
107  }
108  }
109  /* if requested, fill in tail with the position after the last parsed
110  character */
111  if (tail)
112  *tail = next;
113  return d;
114 }
115 
116 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
117 
118 static int strmatch(const char *s, const char *prefix)
119 {
120  int i;
121  for (i=0; prefix[i]; i++) {
122  if (prefix[i] != s[i]) return 0;
123  }
124  /* return 1 only if the s identifier is terminated */
125  return !IS_IDENTIFIER_CHAR(s[i]);
126 }
127 
128 struct AVExpr {
129  enum {
136  } type;
137  double value; // is sign in other types
138  union {
140  double (*func0)(double);
141  double (*func1)(void *, double);
142  double (*func2)(void *, double, double);
143  } a;
144  struct AVExpr *param[2];
145 };
146 
147 static double eval_expr(Parser *p, AVExpr *e)
148 {
149  switch (e->type) {
150  case e_value: return e->value;
151  case e_const: return e->value * p->const_values[e->a.const_index];
152  case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
153  case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
154  case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
155  case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
156  case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
157  case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
158  case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
159  case e_isinf: return e->value * !!isinf(eval_expr(p, e->param[0]));
160  case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
161  case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
162  case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
163  case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
164  case e_not: return e->value * eval_expr(p, e->param[0]) == 0;
165  case e_while: {
166  double d = NAN;
167  while (eval_expr(p, e->param[0]))
168  d=eval_expr(p, e->param[1]);
169  return d;
170  }
171  default: {
172  double d = eval_expr(p, e->param[0]);
173  double d2 = eval_expr(p, e->param[1]);
174  switch (e->type) {
175  case e_mod: return e->value * (d - floor(d/d2)*d2);
176  case e_max: return e->value * (d > d2 ? d : d2);
177  case e_min: return e->value * (d < d2 ? d : d2);
178  case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
179  case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
180  case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
181  case e_pow: return e->value * pow(d, d2);
182  case e_mul: return e->value * (d * d2);
183  case e_div: return e->value * (d / d2);
184  case e_add: return e->value * (d + d2);
185  case e_last:return e->value * d2;
186  case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
187  }
188  }
189  }
190  return NAN;
191 }
192 
193 static int parse_expr(AVExpr **e, Parser *p);
194 
196 {
197  if (!e) return;
198  av_expr_free(e->param[0]);
199  av_expr_free(e->param[1]);
200  av_freep(&e);
201 }
202 
203 static int parse_primary(AVExpr **e, Parser *p)
204 {
205  AVExpr *d = av_mallocz(sizeof(AVExpr));
206  char *next = p->s, *s0 = p->s;
207  int ret, i;
208 
209  if (!d)
210  return AVERROR(ENOMEM);
211 
212  /* number */
213  d->value = av_strtod(p->s, &next);
214  if (next != p->s) {
215  d->type = e_value;
216  p->s= next;
217  *e = d;
218  return 0;
219  }
220  d->value = 1;
221 
222  /* named constants */
223  for (i=0; p->const_names && p->const_names[i]; i++) {
224  if (strmatch(p->s, p->const_names[i])) {
225  p->s+= strlen(p->const_names[i]);
226  d->type = e_const;
227  d->a.const_index = i;
228  *e = d;
229  return 0;
230  }
231  }
232 
233  p->s= strchr(p->s, '(');
234  if (!p->s) {
235  av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
236  p->s= next;
237  av_expr_free(d);
238  return AVERROR(EINVAL);
239  }
240  p->s++; // "("
241  if (*next == '(') { // special case do-nothing
242  av_freep(&d);
243  if ((ret = parse_expr(&d, p)) < 0)
244  return ret;
245  if (p->s[0] != ')') {
246  av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
247  av_expr_free(d);
248  return AVERROR(EINVAL);
249  }
250  p->s++; // ")"
251  *e = d;
252  return 0;
253  }
254  if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
255  av_expr_free(d);
256  return ret;
257  }
258  if (p->s[0]== ',') {
259  p->s++; // ","
260  parse_expr(&d->param[1], p);
261  }
262  if (p->s[0] != ')') {
263  av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
264  av_expr_free(d);
265  return AVERROR(EINVAL);
266  }
267  p->s++; // ")"
268 
269  d->type = e_func0;
270  if (strmatch(next, "sinh" )) d->a.func0 = sinh;
271  else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
272  else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
273  else if (strmatch(next, "sin" )) d->a.func0 = sin;
274  else if (strmatch(next, "cos" )) d->a.func0 = cos;
275  else if (strmatch(next, "tan" )) d->a.func0 = tan;
276  else if (strmatch(next, "atan" )) d->a.func0 = atan;
277  else if (strmatch(next, "asin" )) d->a.func0 = asin;
278  else if (strmatch(next, "acos" )) d->a.func0 = acos;
279  else if (strmatch(next, "exp" )) d->a.func0 = exp;
280  else if (strmatch(next, "log" )) d->a.func0 = log;
281  else if (strmatch(next, "abs" )) d->a.func0 = fabs;
282  else if (strmatch(next, "squish")) d->type = e_squish;
283  else if (strmatch(next, "gauss" )) d->type = e_gauss;
284  else if (strmatch(next, "mod" )) d->type = e_mod;
285  else if (strmatch(next, "max" )) d->type = e_max;
286  else if (strmatch(next, "min" )) d->type = e_min;
287  else if (strmatch(next, "eq" )) d->type = e_eq;
288  else if (strmatch(next, "gte" )) d->type = e_gte;
289  else if (strmatch(next, "gt" )) d->type = e_gt;
290  else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
291  else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
292  else if (strmatch(next, "ld" )) d->type = e_ld;
293  else if (strmatch(next, "isnan" )) d->type = e_isnan;
294  else if (strmatch(next, "isinf" )) d->type = e_isinf;
295  else if (strmatch(next, "st" )) d->type = e_st;
296  else if (strmatch(next, "while" )) d->type = e_while;
297  else if (strmatch(next, "floor" )) d->type = e_floor;
298  else if (strmatch(next, "ceil" )) d->type = e_ceil;
299  else if (strmatch(next, "trunc" )) d->type = e_trunc;
300  else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
301  else if (strmatch(next, "not" )) d->type = e_not;
302  else {
303  for (i=0; p->func1_names && p->func1_names[i]; i++) {
304  if (strmatch(next, p->func1_names[i])) {
305  d->a.func1 = p->funcs1[i];
306  d->type = e_func1;
307  *e = d;
308  return 0;
309  }
310  }
311 
312  for (i=0; p->func2_names && p->func2_names[i]; i++) {
313  if (strmatch(next, p->func2_names[i])) {
314  d->a.func2 = p->funcs2[i];
315  d->type = e_func2;
316  *e = d;
317  return 0;
318  }
319  }
320 
321  av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
322  av_expr_free(d);
323  return AVERROR(EINVAL);
324  }
325 
326  *e = d;
327  return 0;
328 }
329 
330 static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
331 {
332  AVExpr *e = av_mallocz(sizeof(AVExpr));
333  if (!e)
334  return NULL;
335  e->type =type ;
336  e->value =value ;
337  e->param[0] =p0 ;
338  e->param[1] =p1 ;
339  return e;
340 }
341 
342 static int parse_pow(AVExpr **e, Parser *p, int *sign)
343 {
344  *sign= (*p->s == '+') - (*p->s == '-');
345  p->s += *sign&1;
346  return parse_primary(e, p);
347 }
348 
349 static int parse_dB(AVExpr **e, Parser *p, int *sign)
350 {
351  /* do not filter out the negative sign when parsing a dB value.
352  for example, -3dB is not the same as -(3dB) */
353  if (*p->s == '-') {
354  char *next;
355  double av_unused ignored = strtod(p->s, &next);
356  if (next != p->s && next[0] == 'd' && next[1] == 'B') {
357  *sign = 0;
358  return parse_primary(e, p);
359  }
360  }
361  return parse_pow(e, p, sign);
362 }
363 
364 static int parse_factor(AVExpr **e, Parser *p)
365 {
366  int sign, sign2, ret;
367  AVExpr *e0, *e1, *e2;
368  if ((ret = parse_dB(&e0, p, &sign)) < 0)
369  return ret;
370  while(p->s[0]=='^'){
371  e1 = e0;
372  p->s++;
373  if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
374  av_expr_free(e1);
375  return ret;
376  }
377  e0 = new_eval_expr(e_pow, 1, e1, e2);
378  if (!e0) {
379  av_expr_free(e1);
380  av_expr_free(e2);
381  return AVERROR(ENOMEM);
382  }
383  if (e0->param[1]) e0->param[1]->value *= (sign2|1);
384  }
385  if (e0) e0->value *= (sign|1);
386 
387  *e = e0;
388  return 0;
389 }
390 
391 static int parse_term(AVExpr **e, Parser *p)
392 {
393  int ret;
394  AVExpr *e0, *e1, *e2;
395  if ((ret = parse_factor(&e0, p)) < 0)
396  return ret;
397  while (p->s[0]=='*' || p->s[0]=='/') {
398  int c= *p->s++;
399  e1 = e0;
400  if ((ret = parse_factor(&e2, p)) < 0) {
401  av_expr_free(e1);
402  return ret;
403  }
404  e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
405  if (!e0) {
406  av_expr_free(e1);
407  av_expr_free(e2);
408  return AVERROR(ENOMEM);
409  }
410  }
411  *e = e0;
412  return 0;
413 }
414 
415 static int parse_subexpr(AVExpr **e, Parser *p)
416 {
417  int ret;
418  AVExpr *e0, *e1, *e2;
419  if ((ret = parse_term(&e0, p)) < 0)
420  return ret;
421  while (*p->s == '+' || *p->s == '-') {
422  e1 = e0;
423  if ((ret = parse_term(&e2, p)) < 0) {
424  av_expr_free(e1);
425  return ret;
426  }
427  e0 = new_eval_expr(e_add, 1, e1, e2);
428  if (!e0) {
429  av_expr_free(e1);
430  av_expr_free(e2);
431  return AVERROR(ENOMEM);
432  }
433  };
434 
435  *e = e0;
436  return 0;
437 }
438 
439 static int parse_expr(AVExpr **e, Parser *p)
440 {
441  int ret;
442  AVExpr *e0, *e1, *e2;
443  if (p->stack_index <= 0) //protect against stack overflows
444  return AVERROR(EINVAL);
445  p->stack_index--;
446 
447  if ((ret = parse_subexpr(&e0, p)) < 0)
448  return ret;
449  while (*p->s == ';') {
450  p->s++;
451  e1 = e0;
452  if ((ret = parse_subexpr(&e2, p)) < 0) {
453  av_expr_free(e1);
454  return ret;
455  }
456  e0 = new_eval_expr(e_last, 1, e1, e2);
457  if (!e0) {
458  av_expr_free(e1);
459  av_expr_free(e2);
460  return AVERROR(ENOMEM);
461  }
462  };
463 
464  p->stack_index++;
465  *e = e0;
466  return 0;
467 }
468 
469 static int verify_expr(AVExpr *e)
470 {
471  if (!e) return 0;
472  switch (e->type) {
473  case e_value:
474  case e_const: return 1;
475  case e_func0:
476  case e_func1:
477  case e_squish:
478  case e_ld:
479  case e_gauss:
480  case e_isnan:
481  case e_isinf:
482  case e_floor:
483  case e_ceil:
484  case e_trunc:
485  case e_sqrt:
486  case e_not:
487  return verify_expr(e->param[0]);
488  default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
489  }
490 }
491 
492 int av_expr_parse(AVExpr **expr, const char *s,
493  const char * const *const_names,
494  const char * const *func1_names, double (* const *funcs1)(void *, double),
495  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
496  int log_offset, void *log_ctx)
497 {
498  Parser p = { 0 };
499  AVExpr *e = NULL;
500  char *w = av_malloc(strlen(s) + 1);
501  char *wp = w;
502  const char *s0 = s;
503  int ret = 0;
504 
505  if (!w)
506  return AVERROR(ENOMEM);
507 
508  while (*s)
509  if (!av_isspace(*s++)) *wp++ = s[-1];
510  *wp++ = 0;
511 
512  p.class = &class;
513  p.stack_index=100;
514  p.s= w;
516  p.funcs1 = funcs1;
517  p.func1_names = func1_names;
518  p.funcs2 = funcs2;
519  p.func2_names = func2_names;
520  p.log_offset = log_offset;
521  p.log_ctx = log_ctx;
522 
523  if ((ret = parse_expr(&e, &p)) < 0)
524  goto end;
525  if (*p.s) {
526  av_expr_free(e);
527  av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
528  ret = AVERROR(EINVAL);
529  goto end;
530  }
531  if (!verify_expr(e)) {
532  av_expr_free(e);
533  ret = AVERROR(EINVAL);
534  goto end;
535  }
536  *expr = e;
537 end:
538  av_free(w);
539  return ret;
540 }
541 
542 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
543 {
544  Parser p = { 0 };
545 
547  p.opaque = opaque;
548  return eval_expr(&p, e);
549 }
550 
551 int av_expr_parse_and_eval(double *d, const char *s,
552  const char * const *const_names, const double *const_values,
553  const char * const *func1_names, double (* const *funcs1)(void *, double),
554  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
555  void *opaque, int log_offset, void *log_ctx)
556 {
557  AVExpr *e = NULL;
558  int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
559 
560  if (ret < 0) {
561  *d = NAN;
562  return ret;
563  }
564  *d = av_expr_eval(e, const_values, opaque);
565  av_expr_free(e);
566  return isnan(*d) ? AVERROR(EINVAL) : 0;
567 }
568 
569 #ifdef TEST
570 #include <string.h>
571 
572 static const double const_values[] = {
573  M_PI,
574  M_E,
575  0
576 };
577 
578 static const char *const const_names[] = {
579  "PI",
580  "E",
581  0
582 };
583 
584 int main(int argc, char **argv)
585 {
586  int i;
587  double d;
588  const char *const *expr;
589  static const char *const exprs[] = {
590  "",
591  "1;2",
592  "-20",
593  "-PI",
594  "+PI",
595  "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
596  "80G/80Gi",
597  "1k",
598  "1Gi",
599  "1gi",
600  "1GiFoo",
601  "1k+1k",
602  "1Gi*3foo",
603  "foo",
604  "foo(",
605  "foo()",
606  "foo)",
607  "sin",
608  "sin(",
609  "sin()",
610  "sin)",
611  "sin 10",
612  "sin(1,2,3)",
613  "sin(1 )",
614  "1",
615  "1foo",
616  "bar + PI + E + 100f*2 + foo",
617  "13k + 12f - foo(1, 2)",
618  "1gi",
619  "1Gi",
620  "st(0, 123)",
621  "st(1, 123); ld(1)",
622  "lte(0, 1)",
623  "lte(1, 1)",
624  "lte(1, 0)",
625  "lt(0, 1)",
626  "lt(1, 1)",
627  "gt(1, 0)",
628  "gt(2, 7)",
629  "gte(122, 122)",
630  /* compute 1+2+...+N */
631  "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
632  /* compute Fib(N) */
633  "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)",
634  "while(0, 10)",
635  "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
636  "isnan(1)",
637  "isnan(NAN)",
638  "isnan(INF)",
639  "isinf(1)",
640  "isinf(NAN)",
641  "isinf(INF)",
642  "floor(NAN)",
643  "floor(123.123)",
644  "floor(-123.123)",
645  "trunc(123.123)",
646  "trunc(-123.123)",
647  "ceil(123.123)",
648  "ceil(-123.123)",
649  "sqrt(1764)",
650  "isnan(sqrt(-1))",
651  "not(1)",
652  "not(NAN)",
653  "not(0)",
654  "6.0206dB",
655  "-3.0103dB",
656  NULL
657  };
658 
659  for (expr = exprs; *expr; expr++) {
660  printf("Evaluating '%s'\n", *expr);
661  av_expr_parse_and_eval(&d, *expr,
662  const_names, const_values,
663  NULL, NULL, NULL, NULL, NULL, 0, NULL);
664  if (isnan(d))
665  printf("'%s' -> nan\n\n", *expr);
666  else
667  printf("'%s' -> %f\n\n", *expr, d);
668  }
669 
670  av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
671  const_names, const_values,
672  NULL, NULL, NULL, NULL, NULL, 0, NULL);
673  printf("%f == 12.7\n", d);
674  av_expr_parse_and_eval(&d, "80G/80Gi",
675  const_names, const_values,
676  NULL, NULL, NULL, NULL, NULL, 0, NULL);
677  printf("%f == 0.931322575\n", d);
678 
679  if (argc > 1 && !strcmp(argv[1], "-t")) {
680  for (i = 0; i < 1050; i++) {
681  START_TIMER;
682  av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
683  const_names, const_values,
684  NULL, NULL, NULL, NULL, NULL, 0, NULL);
685  STOP_TIMER("av_expr_parse_and_eval");
686  }
687  }
688 
689  return 0;
690 }
691 #endif
#define VARS
Definition: eval.c:51
static int verify_expr(AVExpr *e)
Definition: eval.c:469
const char *const * func1_names
Definition: eval.c:45
struct AVExpr * param[2]
Definition: eval.c:144
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
external API header
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:492
void * log_ctx
Definition: eval.c:50
Macro definitions for various function/variable attributes.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int parse_dB(AVExpr **e, Parser *p, int *sign)
Definition: eval.c:349
static av_always_inline av_const int isnan(float x)
Definition: libm.h:85
static int parse_expr(AVExpr **e, Parser *p)
Definition: eval.c:439
#define b
Definition: input.c:52
Definition: eval.c:38
static double(*const funcs1[])(void *, double)
Definition: vf_lut.c:185
Definition: eval.c:128
static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
Definition: eval.c:330
double strtod(const char *, char **)
union AVExpr::@130 a
const char *const * const_names
Definition: eval.c:43
static const double const_values[]
Definition: opt.c:87
double(* func1)(void *, double)
Definition: eval.c:141
static int parse_pow(AVExpr **e, Parser *p, int *sign)
Definition: eval.c:342
static int parse_factor(AVExpr **e, Parser *p)
Definition: eval.c:364
high precision timer, useful to profile code
av_default_item_name
Definition: eval.c:55
static int parse_subexpr(AVExpr **e, Parser *p)
Definition: eval.c:415
double(* func2)(void *, double, double)
Definition: eval.c:142
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:551
const double * const_values
Definition: eval.c:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define IS_IDENTIFIER_CHAR(c)
Definition: eval.c:116
#define AVERROR(e)
Definition: error.h:43
int const_index
Definition: eval.c:139
static int parse_term(AVExpr **e, Parser *p)
Definition: eval.c:391
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int main(int argc, char **argv)
Definition: avconv.c:2609
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.c:225
static const char *const const_names[]
Definition: opt.c:94
static const int8_t si_prefixes['z'- 'E'+1]
Definition: eval.c:57
#define M_E
Definition: ratecontrol.c:39
static av_always_inline av_const double trunc(double x)
Definition: libm.h:165
static int strmatch(const char *s, const char *prefix)
Definition: eval.c:118
static int parse_primary(AVExpr **e, Parser *p)
Definition: eval.c:203
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
void * opaque
Definition: eval.c:48
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:80
const AVClass * class
Definition: eval.c:39
NULL
Definition: eval.c:55
static double eval_expr(Parser *p, AVExpr *e)
Definition: eval.c:147
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:195
#define START_TIMER
Definition: timer.h:84
char * s
Definition: eval.c:41
Describe the class of an AVClass context structure.
Definition: log.h:33
double(*const funcs1)(void *, double a)
Definition: eval.c:44
const char *const * func2_names
Definition: eval.c:47
enum AVExpr::@129 type
common internal and external API header
double var[VARS]
Definition: eval.c:52
int stack_index
Definition: eval.c:40
#define STOP_TIMER(id)
Definition: timer.h:85
#define NAN
Definition: math.h:28
double(*const funcs2)(void *, double a, double b)
Definition: eval.c:46
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:542
double(* func0)(double)
Definition: eval.c:140
static av_always_inline av_const int isinf(float x)
Definition: libm.h:75
int log_offset
Definition: eval.c:49
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
#define av_unused
Definition: attributes.h:86
double value
Definition: eval.c:137
simple arithmetic expression evaluator