FFmpeg  4.4.7
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 "attributes.h"
31 #include "avutil.h"
32 #include "common.h"
33 #include "eval.h"
34 #include "ffmath.h"
35 #include "internal.h"
36 #include "log.h"
37 #include "mathematics.h"
38 #include "time.h"
39 #include "avstring.h"
40 #include "timer.h"
41 #include "reverse.h"
42 
43 #define MAX_DEPTH 100
44 
45 typedef struct Parser {
46  const AVClass *class;
48  char *s;
49  const double *const_values;
50  const char * const *const_names; // NULL terminated
51  double (* const *funcs1)(void *, double a); // NULL terminated
52  const char * const *func1_names; // NULL terminated
53  double (* const *funcs2)(void *, double a, double b); // NULL terminated
54  const char * const *func2_names; // NULL terminated
55  void *opaque;
57  void *log_ctx;
58 #define VARS 10
59  double *var;
60 } Parser;
61 
62 static const AVClass eval_class = {
63  .class_name = "Eval",
64  .item_name = av_default_item_name,
65  .option = NULL,
66  .version = LIBAVUTIL_VERSION_INT,
67  .log_level_offset_offset = offsetof(Parser, log_offset),
68  .parent_log_context_offset = offsetof(Parser, log_ctx),
69 };
70 
71 static const struct {
72  double bin_val;
73  double dec_val;
74  int8_t exp;
75 } si_prefixes['z' - 'E' + 1] = {
76  ['y'-'E']= { 8.271806125530276749e-25, 1e-24, -24 },
77  ['z'-'E']= { 8.4703294725430034e-22, 1e-21, -21 },
78  ['a'-'E']= { 8.6736173798840355e-19, 1e-18, -18 },
79  ['f'-'E']= { 8.8817841970012523e-16, 1e-15, -15 },
80  ['p'-'E']= { 9.0949470177292824e-13, 1e-12, -12 },
81  ['n'-'E']= { 9.3132257461547852e-10, 1e-9, -9 },
82  ['u'-'E']= { 9.5367431640625e-7, 1e-6, -6 },
83  ['m'-'E']= { 9.765625e-4, 1e-3, -3 },
84  ['c'-'E']= { 9.8431332023036951e-3, 1e-2, -2 },
85  ['d'-'E']= { 9.921256574801246e-2, 1e-1, -1 },
86  ['h'-'E']= { 1.0159366732596479e2, 1e2, 2 },
87  ['k'-'E']= { 1.024e3, 1e3, 3 },
88  ['K'-'E']= { 1.024e3, 1e3, 3 },
89  ['M'-'E']= { 1.048576e6, 1e6, 6 },
90  ['G'-'E']= { 1.073741824e9, 1e9, 9 },
91  ['T'-'E']= { 1.099511627776e12, 1e12, 12 },
92  ['P'-'E']= { 1.125899906842624e15, 1e15, 15 },
93  ['E'-'E']= { 1.152921504606847e18, 1e18, 18 },
94  ['Z'-'E']= { 1.1805916207174113e21, 1e21, 21 },
95  ['Y'-'E']= { 1.2089258196146292e24, 1e24, 24 },
96 };
97 
98 static const struct {
99  const char *name;
100  double value;
101 } constants[] = {
102  { "E", M_E },
103  { "PI", M_PI },
104  { "PHI", M_PHI },
105  { "QP2LAMBDA", FF_QP2LAMBDA },
106 };
107 
108 double av_strtod(const char *numstr, char **tail)
109 {
110  double d;
111  char *next;
112  if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
113  d = strtoul(numstr, &next, 16);
114  } else
115  d = strtod(numstr, &next);
116  /* if parsing succeeded, check for and interpret postfixes */
117  if (next!=numstr) {
118  if (next[0] == 'd' && next[1] == 'B') {
119  /* treat dB as decibels instead of decibytes */
120  d = ff_exp10(d / 20);
121  next += 2;
122  } else if (*next >= 'E' && *next <= 'z') {
123  int e= si_prefixes[*next - 'E'].exp;
124  if (e) {
125  if (next[1] == 'i') {
126  d*= si_prefixes[*next - 'E'].bin_val;
127  next+=2;
128  } else {
129  d*= si_prefixes[*next - 'E'].dec_val;
130  next++;
131  }
132  }
133  }
134 
135  if (*next=='B') {
136  d*=8;
137  next++;
138  }
139  }
140  /* if requested, fill in tail with the position after the last parsed
141  character */
142  if (tail)
143  *tail = next;
144  return d;
145 }
146 
147 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
148 
149 static int strmatch(const char *s, const char *prefix)
150 {
151  int i;
152  for (i=0; prefix[i]; i++) {
153  if (prefix[i] != s[i]) return 0;
154  }
155  /* return 1 only if the s identifier is terminated */
156  return !IS_IDENTIFIER_CHAR(s[i]);
157 }
158 
159 struct AVExpr {
160  enum {
169  } type;
170  double value; // is sign in other types
172  union {
173  double (*func0)(double);
174  double (*func1)(void *, double);
175  double (*func2)(void *, double, double);
176  } a;
177  struct AVExpr *param[3];
178  double *var;
179  int depth;
180 };
181 
182 static double etime(double v)
183 {
184  return av_gettime() * 0.000001;
185 }
186 
187 static double eval_expr(Parser *p, AVExpr *e)
188 {
189  switch (e->type) {
190  case e_value: return e->value;
191  case e_const: return e->value * p->const_values[e->const_index];
192  case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
193  case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
194  case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
195  case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
196  case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
197  case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
198  case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
199  case e_isinf: return e->value * !!isinf(eval_expr(p, e->param[0]));
200  case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
201  case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
202  case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
203  case e_round: return e->value * round(eval_expr(p, e->param[0]));
204  case e_sgn: return e->value * FFDIFFSIGN(eval_expr(p, e->param[0]), 0);
205  case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
206  case e_not: return e->value * (eval_expr(p, e->param[0]) == 0);
207  case e_if: return e->value * (eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
208  e->param[2] ? eval_expr(p, e->param[2]) : 0);
209  case e_ifnot: return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
210  e->param[2] ? eval_expr(p, e->param[2]) : 0);
211  case e_clip: {
212  double x = eval_expr(p, e->param[0]);
213  double min = eval_expr(p, e->param[1]), max = eval_expr(p, e->param[2]);
214  if (isnan(min) || isnan(max) || isnan(x) || min > max)
215  return NAN;
216  return e->value * av_clipd(eval_expr(p, e->param[0]), min, max);
217  }
218  case e_between: {
219  double d = eval_expr(p, e->param[0]);
220  return e->value * (d >= eval_expr(p, e->param[1]) &&
221  d <= eval_expr(p, e->param[2]));
222  }
223  case e_lerp: {
224  double v0 = eval_expr(p, e->param[0]);
225  double v1 = eval_expr(p, e->param[1]);
226  double f = eval_expr(p, e->param[2]);
227  return v0 + (v1 - v0) * f;
228  }
229  case e_print: {
230  double x = eval_expr(p, e->param[0]);
231  int level = e->param[1] ? av_clip(eval_expr(p, e->param[1]), INT_MIN, INT_MAX) : AV_LOG_INFO;
232  av_log(p, level, "%f\n", x);
233  return x;
234  }
235  case e_random:{
236  int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
237  uint64_t r= isnan(p->var[idx]) ? 0 : p->var[idx];
238  r= r*1664525+1013904223;
239  p->var[idx]= r;
240  return e->value * (r * (1.0/UINT64_MAX));
241  }
242  case e_while: {
243  double d = NAN;
244  while (eval_expr(p, e->param[0]))
245  d=eval_expr(p, e->param[1]);
246  return d;
247  }
248  case e_taylor: {
249  double t = 1, d = 0, v;
250  double x = eval_expr(p, e->param[1]);
251  int id = e->param[2] ? av_clip(eval_expr(p, e->param[2]), 0, VARS-1) : 0;
252  int i;
253  double var0 = p->var[id];
254  for(i=0; i<1000; i++) {
255  double ld = d;
256  p->var[id] = i;
257  v = eval_expr(p, e->param[0]);
258  d += t*v;
259  if(ld==d && v)
260  break;
261  t *= x / (i+1);
262  }
263  p->var[id] = var0;
264  return d;
265  }
266  case e_root: {
267  int i, j;
268  double low = -1, high = -1, v, low_v = -DBL_MAX, high_v = DBL_MAX;
269  double var0 = p->var[0];
270  double x_max = eval_expr(p, e->param[1]);
271  for(i=-1; i<1024; i++) {
272  if(i<255) {
273  p->var[0] = ff_reverse[i&255]*x_max/255;
274  } else {
275  p->var[0] = x_max*pow(0.9, i-255);
276  if (i&1) p->var[0] *= -1;
277  if (i&2) p->var[0] += low;
278  else p->var[0] += high;
279  }
280  v = eval_expr(p, e->param[0]);
281  if (v<=0 && v>low_v) {
282  low = p->var[0];
283  low_v = v;
284  }
285  if (v>=0 && v<high_v) {
286  high = p->var[0];
287  high_v = v;
288  }
289  if (low>=0 && high>=0){
290  for (j=0; j<1000; j++) {
291  p->var[0] = (low+high)*0.5;
292  if (low == p->var[0] || high == p->var[0])
293  break;
294  v = eval_expr(p, e->param[0]);
295  if (v<=0) low = p->var[0];
296  if (v>=0) high= p->var[0];
297  if (isnan(v)) {
298  low = high = v;
299  break;
300  }
301  }
302  break;
303  }
304  }
305  p->var[0] = var0;
306  return -low_v<high_v ? low : high;
307  }
308  default: {
309  double d = eval_expr(p, e->param[0]);
310  double d2 = eval_expr(p, e->param[1]);
311  switch (e->type) {
312  case e_mod: return e->value * (d - floor(d2 ? d / d2 : d * INFINITY) * d2);
313  case e_gcd: return e->value * av_gcd(d,d2);
314  case e_max: return e->value * (d > d2 ? d : d2);
315  case e_min: return e->value * (d < d2 ? d : d2);
316  case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
317  case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
318  case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
319  case e_lt: return e->value * (d < d2 ? 1.0 : 0.0);
320  case e_lte: return e->value * (d <= d2 ? 1.0 : 0.0);
321  case e_pow: return e->value * pow(d, d2);
322  case e_mul: return e->value * (d * d2);
323  case e_div: return e->value * (d2 ? (d / d2) : d * INFINITY);
324  case e_add: return e->value * (d + d2);
325  case e_last:return e->value * d2;
326  case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
327  case e_hypot:return e->value * hypot(d, d2);
328  case e_atan2:return e->value * atan2(d, d2);
329  case e_bitand: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d & (long int)d2);
330  case e_bitor: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d | (long int)d2);
331  }
332  }
333  }
334  return NAN;
335 }
336 
337 static int parse_expr(AVExpr **e, Parser *p);
338 
340 {
341  if (!e) return;
342  av_expr_free(e->param[0]);
343  av_expr_free(e->param[1]);
344  av_expr_free(e->param[2]);
345  av_freep(&e->var);
346  av_freep(&e);
347 }
348 
349 static int parse_primary(AVExpr **e, Parser *p)
350 {
351  AVExpr *d = av_mallocz(sizeof(AVExpr));
352  char *next = p->s, *s0 = p->s;
353  int ret, i;
354 
355  if (!d)
356  return AVERROR(ENOMEM);
357 
358  /* number */
359  d->value = av_strtod(p->s, &next);
360  if (next != p->s) {
361  d->type = e_value;
362  p->s= next;
363  *e = d;
364  return 0;
365  }
366  d->value = 1;
367 
368  /* named constants */
369  for (i=0; p->const_names && p->const_names[i]; i++) {
370  if (strmatch(p->s, p->const_names[i])) {
371  p->s+= strlen(p->const_names[i]);
372  d->type = e_const;
373  d->const_index = i;
374  *e = d;
375  return 0;
376  }
377  }
378  for (i = 0; i < FF_ARRAY_ELEMS(constants); i++) {
379  if (strmatch(p->s, constants[i].name)) {
380  p->s += strlen(constants[i].name);
381  d->type = e_value;
382  d->value = constants[i].value;
383  *e = d;
384  return 0;
385  }
386  }
387 
388  p->s= strchr(p->s, '(');
389  if (!p->s) {
390  av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
391  p->s= next;
392  av_expr_free(d);
393  return AVERROR(EINVAL);
394  }
395  p->s++; // "("
396  if (*next == '(') { // special case do-nothing
397  av_freep(&d);
398  if ((ret = parse_expr(&d, p)) < 0)
399  return ret;
400  if (p->s[0] != ')') {
401  av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
402  av_expr_free(d);
403  return AVERROR(EINVAL);
404  }
405  p->s++; // ")"
406  *e = d;
407  return 0;
408  }
409  if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
410  av_expr_free(d);
411  return ret;
412  }
413  if (p->s[0]== ',') {
414  p->s++; // ","
415  parse_expr(&d->param[1], p);
416  }
417  if (p->s[0]== ',') {
418  p->s++; // ","
419  parse_expr(&d->param[2], p);
420  }
421  if (p->s[0] != ')') {
422  av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
423  av_expr_free(d);
424  return AVERROR(EINVAL);
425  }
426  p->s++; // ")"
427 
428  for (int i = 0; i<3; i++)
429  if (d->param[i])
430  d->depth = FFMAX(d->depth, d->param[i]->depth+1);
431  if (d->depth > MAX_DEPTH) {
432  av_expr_free(d);
433  return AVERROR(EINVAL);
434  }
435 
436  d->type = e_func0;
437  if (strmatch(next, "sinh" )) d->a.func0 = sinh;
438  else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
439  else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
440  else if (strmatch(next, "sin" )) d->a.func0 = sin;
441  else if (strmatch(next, "cos" )) d->a.func0 = cos;
442  else if (strmatch(next, "tan" )) d->a.func0 = tan;
443  else if (strmatch(next, "atan" )) d->a.func0 = atan;
444  else if (strmatch(next, "asin" )) d->a.func0 = asin;
445  else if (strmatch(next, "acos" )) d->a.func0 = acos;
446  else if (strmatch(next, "exp" )) d->a.func0 = exp;
447  else if (strmatch(next, "log" )) d->a.func0 = log;
448  else if (strmatch(next, "abs" )) d->a.func0 = fabs;
449  else if (strmatch(next, "time" )) d->a.func0 = etime;
450  else if (strmatch(next, "squish")) d->type = e_squish;
451  else if (strmatch(next, "gauss" )) d->type = e_gauss;
452  else if (strmatch(next, "mod" )) d->type = e_mod;
453  else if (strmatch(next, "max" )) d->type = e_max;
454  else if (strmatch(next, "min" )) d->type = e_min;
455  else if (strmatch(next, "eq" )) d->type = e_eq;
456  else if (strmatch(next, "gte" )) d->type = e_gte;
457  else if (strmatch(next, "gt" )) d->type = e_gt;
458  else if (strmatch(next, "lte" )) d->type = e_lte;
459  else if (strmatch(next, "lt" )) d->type = e_lt;
460  else if (strmatch(next, "ld" )) d->type = e_ld;
461  else if (strmatch(next, "isnan" )) d->type = e_isnan;
462  else if (strmatch(next, "isinf" )) d->type = e_isinf;
463  else if (strmatch(next, "st" )) d->type = e_st;
464  else if (strmatch(next, "while" )) d->type = e_while;
465  else if (strmatch(next, "taylor")) d->type = e_taylor;
466  else if (strmatch(next, "root" )) d->type = e_root;
467  else if (strmatch(next, "floor" )) d->type = e_floor;
468  else if (strmatch(next, "ceil" )) d->type = e_ceil;
469  else if (strmatch(next, "trunc" )) d->type = e_trunc;
470  else if (strmatch(next, "round" )) d->type = e_round;
471  else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
472  else if (strmatch(next, "not" )) d->type = e_not;
473  else if (strmatch(next, "pow" )) d->type = e_pow;
474  else if (strmatch(next, "print" )) d->type = e_print;
475  else if (strmatch(next, "random")) d->type = e_random;
476  else if (strmatch(next, "hypot" )) d->type = e_hypot;
477  else if (strmatch(next, "gcd" )) d->type = e_gcd;
478  else if (strmatch(next, "if" )) d->type = e_if;
479  else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
480  else if (strmatch(next, "bitand")) d->type = e_bitand;
481  else if (strmatch(next, "bitor" )) d->type = e_bitor;
482  else if (strmatch(next, "between"))d->type = e_between;
483  else if (strmatch(next, "clip" )) d->type = e_clip;
484  else if (strmatch(next, "atan2" )) d->type = e_atan2;
485  else if (strmatch(next, "lerp" )) d->type = e_lerp;
486  else if (strmatch(next, "sgn" )) d->type = e_sgn;
487  else {
488  for (i=0; p->func1_names && p->func1_names[i]; i++) {
489  if (strmatch(next, p->func1_names[i])) {
490  d->a.func1 = p->funcs1[i];
491  d->type = e_func1;
492  d->const_index = i;
493  *e = d;
494  return 0;
495  }
496  }
497 
498  for (i=0; p->func2_names && p->func2_names[i]; i++) {
499  if (strmatch(next, p->func2_names[i])) {
500  d->a.func2 = p->funcs2[i];
501  d->type = e_func2;
502  d->const_index = i;
503  *e = d;
504  return 0;
505  }
506  }
507 
508  av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
509  av_expr_free(d);
510  return AVERROR(EINVAL);
511  }
512 
513  *e = d;
514  return 0;
515 }
516 
517 static AVExpr *make_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
518 {
519  int depth = FFMAX(p0->depth, p1->depth) + 1;
520  if (depth > MAX_DEPTH)
521  return NULL;
522  AVExpr *e = av_mallocz(sizeof(AVExpr));
523  if (!e)
524  return NULL;
525  e->type =type ;
526  e->value =value ;
527  e->param[0] =p0 ;
528  e->param[1] =p1 ;
529  e->depth = depth;
530  return e;
531 }
532 
533 static int parse_pow(AVExpr **e, Parser *p, int *sign)
534 {
535  *sign= (*p->s == '+') - (*p->s == '-');
536  p->s += *sign&1;
537  return parse_primary(e, p);
538 }
539 
540 static int parse_dB(AVExpr **e, Parser *p, int *sign)
541 {
542  /* do not filter out the negative sign when parsing a dB value.
543  for example, -3dB is not the same as -(3dB) */
544  if (*p->s == '-') {
545  char *next;
546  double av_unused ignored = strtod(p->s, &next);
547  if (next != p->s && next[0] == 'd' && next[1] == 'B') {
548  *sign = 0;
549  return parse_primary(e, p);
550  }
551  }
552  return parse_pow(e, p, sign);
553 }
554 
555 static int parse_factor(AVExpr **e, Parser *p)
556 {
557  int sign, sign2, ret;
558  AVExpr *e0, *e1, *e2;
559  if ((ret = parse_dB(&e0, p, &sign)) < 0)
560  return ret;
561  while(p->s[0]=='^'){
562  e1 = e0;
563  p->s++;
564  if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
565  av_expr_free(e1);
566  return ret;
567  }
568  e0 = make_eval_expr(e_pow, 1, e1, e2);
569  if (!e0) {
570  av_expr_free(e1);
571  av_expr_free(e2);
572  return AVERROR(ENOMEM);
573  }
574  if (e0->param[1]) e0->param[1]->value *= (sign2|1);
575  }
576  if (e0) e0->value *= (sign|1);
577 
578  *e = e0;
579  return 0;
580 }
581 
582 static int parse_term(AVExpr **e, Parser *p)
583 {
584  int ret;
585  AVExpr *e0, *e1, *e2;
586  if ((ret = parse_factor(&e0, p)) < 0)
587  return ret;
588  while (p->s[0]=='*' || p->s[0]=='/') {
589  int c= *p->s++;
590  e1 = e0;
591  if ((ret = parse_factor(&e2, p)) < 0) {
592  av_expr_free(e1);
593  return ret;
594  }
595  e0 = make_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
596  if (!e0) {
597  av_expr_free(e1);
598  av_expr_free(e2);
599  return AVERROR(ENOMEM);
600  }
601  }
602  *e = e0;
603  return 0;
604 }
605 
606 static int parse_subexpr(AVExpr **e, Parser *p)
607 {
608  int ret;
609  AVExpr *e0, *e1, *e2;
610  if ((ret = parse_term(&e0, p)) < 0)
611  return ret;
612  while (*p->s == '+' || *p->s == '-') {
613  e1 = e0;
614  if ((ret = parse_term(&e2, p)) < 0) {
615  av_expr_free(e1);
616  return ret;
617  }
618  e0 = make_eval_expr(e_add, 1, e1, e2);
619  if (!e0) {
620  av_expr_free(e1);
621  av_expr_free(e2);
622  return AVERROR(ENOMEM);
623  }
624  };
625 
626  *e = e0;
627  return 0;
628 }
629 
630 static int parse_expr(AVExpr **e, Parser *p)
631 {
632  int ret;
633  AVExpr *e0, *e1, *e2;
634  if (p->stack_index <= 0) //protect against stack overflows
635  return AVERROR(EINVAL);
636  p->stack_index--;
637 
638  if ((ret = parse_subexpr(&e0, p)) < 0)
639  return ret;
640  while (*p->s == ';') {
641  p->s++;
642  e1 = e0;
643  if ((ret = parse_subexpr(&e2, p)) < 0) {
644  av_expr_free(e1);
645  return ret;
646  }
647  e0 = make_eval_expr(e_last, 1, e1, e2);
648  if (!e0) {
649  av_expr_free(e1);
650  av_expr_free(e2);
651  return AVERROR(ENOMEM);
652  }
653  };
654 
655  p->stack_index++;
656  *e = e0;
657  return 0;
658 }
659 
660 static int verify_expr(AVExpr *e)
661 {
662  if (!e) return 0;
663  switch (e->type) {
664  case e_value:
665  case e_const: return 1;
666  case e_func0:
667  case e_func1:
668  case e_squish:
669  case e_ld:
670  case e_gauss:
671  case e_isnan:
672  case e_isinf:
673  case e_floor:
674  case e_ceil:
675  case e_trunc:
676  case e_round:
677  case e_sqrt:
678  case e_not:
679  case e_random:
680  case e_sgn:
681  return verify_expr(e->param[0]) && !e->param[1];
682  case e_print:
683  return verify_expr(e->param[0])
684  && (!e->param[1] || verify_expr(e->param[1]));
685  case e_if:
686  case e_ifnot:
687  case e_taylor:
688  return verify_expr(e->param[0]) && verify_expr(e->param[1])
689  && (!e->param[2] || verify_expr(e->param[2]));
690  case e_between:
691  case e_clip:
692  case e_lerp:
693  return verify_expr(e->param[0]) &&
694  verify_expr(e->param[1]) &&
695  verify_expr(e->param[2]);
696  default: return verify_expr(e->param[0]) && verify_expr(e->param[1]) && !e->param[2];
697  }
698 }
699 
700 int av_expr_parse(AVExpr **expr, const char *s,
701  const char * const *const_names,
702  const char * const *func1_names, double (* const *funcs1)(void *, double),
703  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
704  int log_offset, void *log_ctx)
705 {
706  Parser p = { 0 };
707  AVExpr *e = NULL;
708  char *w = av_malloc(strlen(s) + 1);
709  char *wp = w;
710  const char *s0 = s;
711  int ret = 0;
712 
713  if (!w)
714  return AVERROR(ENOMEM);
715 
716  while (*s)
717  if (!av_isspace(*s++)) *wp++ = s[-1];
718  *wp++ = 0;
719 
720  p.class = &eval_class;
721  p.stack_index=100;
722  p.s= w;
724  p.funcs1 = funcs1;
726  p.funcs2 = funcs2;
728  p.log_offset = log_offset;
729  p.log_ctx = log_ctx;
730 
731  if ((ret = parse_expr(&e, &p)) < 0)
732  goto end;
733  if (*p.s) {
734  av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
735  ret = AVERROR(EINVAL);
736  goto end;
737  }
738  if (!verify_expr(e)) {
739  ret = AVERROR(EINVAL);
740  goto end;
741  }
742  e->var= av_mallocz(sizeof(double) *VARS);
743  if (!e->var) {
744  ret = AVERROR(ENOMEM);
745  goto end;
746  }
747  *expr = e;
748  e = NULL;
749 end:
750  av_expr_free(e);
751  av_free(w);
752  return ret;
753 }
754 
755 static int expr_count(AVExpr *e, unsigned *counter, int size, int type)
756 {
757  int i;
758 
759  if (!e || !counter || !size)
760  return AVERROR(EINVAL);
761 
762  for (i = 0; e->type != type && i < 3 && e->param[i]; i++)
763  expr_count(e->param[i], counter, size, type);
764 
765  if (e->type == type && e->const_index < size)
766  counter[e->const_index]++;
767 
768  return 0;
769 }
770 
771 int av_expr_count_vars(AVExpr *e, unsigned *counter, int size)
772 {
773  return expr_count(e, counter, size, e_const);
774 }
775 
776 int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg)
777 {
778  return expr_count(e, counter, size, ((int[]){e_const, e_func1, e_func2})[arg]);
779 }
780 
781 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
782 {
783  Parser p = { 0 };
784  p.var= e->var;
785 
787  p.opaque = opaque;
788  return eval_expr(&p, e);
789 }
790 
791 int av_expr_parse_and_eval(double *d, const char *s,
792  const char * const *const_names, const double *const_values,
793  const char * const *func1_names, double (* const *funcs1)(void *, double),
794  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
795  void *opaque, int log_offset, void *log_ctx)
796 {
797  AVExpr *e = NULL;
798  int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
799 
800  if (ret < 0) {
801  *d = NAN;
802  return ret;
803  }
804  *d = av_expr_eval(e, const_values, opaque);
805  av_expr_free(e);
806  return isnan(*d) ? AVERROR(EINVAL) : 0;
807 }
static const char *const func2_names[]
Definition: af_afftfilt.c:120
Macro definitions for various function/variable attributes.
#define av_unused
Definition: attributes.h:131
Convenience header that includes libavutil's core.
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
common internal and external API header
#define av_clipd
Definition: common.h:173
#define av_clip
Definition: common.h:122
#define FFMAX(a, b)
Definition: common.h:103
#define FFDIFFSIGN(x, y)
Comparator.
Definition: common.h:101
#define NULL
Definition: coverity.c:32
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
static __device__ float trunc(float a)
Definition: cuda_runtime.h:179
#define max(a, b)
Definition: cuda_runtime.h:33
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:339
static int strmatch(const char *s, const char *prefix)
Definition: eval.c:149
#define VARS
Definition: eval.c:58
int av_expr_count_vars(AVExpr *e, unsigned *counter, int size)
Track the presence of variables and their number of occurrences in a parsed expression.
Definition: eval.c:771
static int parse_pow(AVExpr **e, Parser *p, int *sign)
Definition: eval.c:533
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:781
#define MAX_DEPTH
Definition: eval.c:43
double dec_val
Definition: eval.c:73
static int parse_primary(AVExpr **e, Parser *p)
Definition: eval.c:349
static int parse_term(AVExpr **e, Parser *p)
Definition: eval.c:582
static int parse_dB(AVExpr **e, Parser *p, int *sign)
Definition: eval.c:540
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:108
#define IS_IDENTIFIER_CHAR(c)
Definition: eval.c:147
static double etime(double v)
Definition: eval.c:182
const char * name
Definition: eval.c:99
static const struct @291 constants[]
static int parse_subexpr(AVExpr **e, Parser *p)
Definition: eval.c:606
static int parse_expr(AVExpr **e, Parser *p)
Definition: eval.c:630
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:791
static double eval_expr(Parser *p, AVExpr *e)
Definition: eval.c:187
static const struct @290 si_prefixes[ 'z' - 'E'+1]
double bin_val
Definition: eval.c:72
static int parse_factor(AVExpr **e, Parser *p)
Definition: eval.c:555
static int verify_expr(AVExpr *e)
Definition: eval.c:660
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:700
static const AVClass eval_class
Definition: eval.c:62
double value
Definition: eval.c:100
static int expr_count(AVExpr *e, unsigned *counter, int size, int type)
Definition: eval.c:755
static AVExpr * make_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
Definition: eval.c:517
int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg)
Track the presence of user provided functions and their number of occurrences in a parsed expression.
Definition: eval.c:776
int8_t exp
Definition: eval.c:74
simple arithmetic expression evaluator
enum AVCodecID id
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
int
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:227
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
cl_device_type type
int i
Definition: input.c:407
const char * arg
Definition: jacosubdec.c:66
common internal API header
const uint8_t ff_reverse[256]
Definition: reverse.c:23
#define isinf(x)
Definition: libm.h:317
#define isnan(x)
Definition: libm.h:340
static av_const double hypot(double x, double y)
Definition: libm.h:366
static av_always_inline av_const double round(double x)
Definition: libm.h:444
uint8_t w
Definition: llviddspenc.c:39
#define M_PHI
Definition: mathematics.h:49
#define NAN
Definition: mathematics.h:64
#define INFINITY
Definition: mathematics.h:67
#define M_E
Definition: mathematics.h:37
#define M_PI
Definition: mathematics.h:52
#define v0
Definition: regdef.h:26
#define s0
Definition: regdef.h:37
#define FF_ARRAY_ELEMS(a)
double strtod(const char *, char **)
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
Definition: eval.c:159
double value
Definition: eval.c:170
double(* func0)(double)
Definition: eval.c:173
union AVExpr::@293 a
enum AVExpr::@292 type
int depth
Definition: eval.c:179
int const_index
Definition: eval.c:171
@ e_mod
Definition: eval.c:163
@ e_if
Definition: eval.c:167
@ e_print
Definition: eval.c:167
@ e_bitor
Definition: eval.c:167
@ e_hypot
Definition: eval.c:166
@ e_trunc
Definition: eval.c:165
@ e_gt
Definition: eval.c:163
@ e_sgn
Definition: eval.c:168
@ e_lerp
Definition: eval.c:167
@ e_ifnot
Definition: eval.c:167
@ e_lte
Definition: eval.c:163
@ e_func0
Definition: eval.c:161
@ e_gcd
Definition: eval.c:166
@ e_isinf
Definition: eval.c:162
@ e_min
Definition: eval.c:163
@ e_sqrt
Definition: eval.c:166
@ e_add
Definition: eval.c:164
@ e_bitand
Definition: eval.c:167
@ e_round
Definition: eval.c:165
@ e_random
Definition: eval.c:166
@ e_pow
Definition: eval.c:164
@ e_not
Definition: eval.c:166
@ e_lt
Definition: eval.c:163
@ e_clip
Definition: eval.c:167
@ e_gauss
Definition: eval.c:162
@ e_between
Definition: eval.c:167
@ e_st
Definition: eval.c:165
@ e_isnan
Definition: eval.c:162
@ e_func2
Definition: eval.c:161
@ e_last
Definition: eval.c:165
@ e_root
Definition: eval.c:165
@ e_while
Definition: eval.c:165
@ e_atan2
Definition: eval.c:167
@ e_ld
Definition: eval.c:162
@ e_func1
Definition: eval.c:161
@ e_div
Definition: eval.c:164
@ e_max
Definition: eval.c:163
@ e_eq
Definition: eval.c:163
@ e_floor
Definition: eval.c:165
@ e_gte
Definition: eval.c:163
@ e_squish
Definition: eval.c:162
@ e_const
Definition: eval.c:161
@ e_taylor
Definition: eval.c:165
@ e_mul
Definition: eval.c:164
@ e_value
Definition: eval.c:161
@ e_ceil
Definition: eval.c:165
double(* func1)(void *, double)
Definition: eval.c:174
struct AVExpr * param[3]
Definition: eval.c:177
double(* func2)(void *, double, double)
Definition: eval.c:175
double * var
Definition: eval.c:178
Definition: eval.c:45
double(*const funcs2)(void *, double a, double b)
Definition: eval.c:53
void * opaque
Definition: eval.c:55
const char *const * func2_names
Definition: eval.c:54
const double * const_values
Definition: eval.c:49
const AVClass * class
Definition: eval.c:46
const char *const * func1_names
Definition: eval.c:52
double(*const funcs1)(void *, double a)
Definition: eval.c:51
double * var
Definition: eval.c:59
int stack_index
Definition: eval.c:47
int log_offset
Definition: eval.c:56
const char *const * const_names
Definition: eval.c:50
char * s
Definition: eval.c:48
void * log_ctx
Definition: eval.c:57
uint8_t level
Definition: svq3.c:206
#define av_free(p)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static const double const_values[]
Definition: eval.c:28
static const char *const const_names[]
Definition: eval.c:34
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int size
const char * b
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:116
static double(*const funcs1[])(void *, double)
Definition: vf_lut.c:200
static const char *const func1_names[]
Definition: vf_rotate.c:196
float min
static double c[64]