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