Statistics
| Branch: | Revision:

ffmpeg / libavutil / eval.c @ 2b59fbe9

History | View | Annotate | Download (17 KB)

1 3aa102be Michael Niedermayer
/*
2 3cedeeca Michael Niedermayer
 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
3 b5c5a86b Oded Shimon
 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 3aa102be Michael Niedermayer
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 3aa102be Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 3aa102be Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 3aa102be Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 3aa102be Michael Niedermayer
 */
21
22 983e3246 Michael Niedermayer
/**
23 ba87f080 Diego Biurrun
 * @file
24 983e3246 Michael Niedermayer
 * simple arithmetic expression evaluator.
25
 *
26 3aa102be Michael Niedermayer
 * see http://joe.hotchkiss.com/programming/eval/eval.html
27
 */
28
29 6c71d2c1 Stefano Sabatini
#include "libavutil/avutil.h"
30 9a88c1df Måns Rullgård
#include "eval.h"
31
32 0cd28d94 Stefano Sabatini
typedef struct Parser {
33 2b65bb45 Michael Niedermayer
    const AVClass *class;
34 3aa102be Michael Niedermayer
    int stack_index;
35
    char *s;
36 9711439b Stefano Sabatini
    const double *const_values;
37
    const char * const *const_names;          // NULL terminated
38
    double (* const *funcs1)(void *, double a);           // NULL terminated
39
    const char * const *func1_names;          // NULL terminated
40
    double (* const *funcs2)(void *, double a, double b); // NULL terminated
41
    const char * const *func2_names;          // NULL terminated
42 3aa102be Michael Niedermayer
    void *opaque;
43 2b65bb45 Michael Niedermayer
    int log_offset;
44
    void *log_ctx;
45 835954e3 Michael Niedermayer
#define VARS 10
46
    double var[VARS];
47 3aa102be Michael Niedermayer
} Parser;
48
49 2b65bb45 Michael Niedermayer
static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
50
51 0cd28d94 Stefano Sabatini
static const int8_t si_prefixes['z' - 'E' + 1] = {
52 95c99430 Panagiotis Issaris
    ['y'-'E']= -24,
53
    ['z'-'E']= -21,
54
    ['a'-'E']= -18,
55
    ['f'-'E']= -15,
56
    ['p'-'E']= -12,
57
    ['n'-'E']= - 9,
58
    ['u'-'E']= - 6,
59
    ['m'-'E']= - 3,
60
    ['c'-'E']= - 2,
61
    ['d'-'E']= - 1,
62
    ['h'-'E']=   2,
63
    ['k'-'E']=   3,
64
    ['K'-'E']=   3,
65
    ['M'-'E']=   6,
66
    ['G'-'E']=   9,
67
    ['T'-'E']=  12,
68
    ['P'-'E']=  15,
69
    ['E'-'E']=  18,
70
    ['Z'-'E']=  21,
71
    ['Y'-'E']=  24,
72
};
73
74 0cd28d94 Stefano Sabatini
double av_strtod(const char *numstr, char **tail)
75
{
76 95c99430 Panagiotis Issaris
    double d;
77
    char *next;
78 e877eaac Stefano Sabatini
    d = strtod(numstr, &next);
79 95c99430 Panagiotis Issaris
    /* if parsing succeeded, check for and interpret postfixes */
80 e877eaac Stefano Sabatini
    if (next!=numstr) {
81 0cd28d94 Stefano Sabatini
        if (*next >= 'E' && *next <= 'z') {
82 95c99430 Panagiotis Issaris
            int e= si_prefixes[*next - 'E'];
83 0cd28d94 Stefano Sabatini
            if (e) {
84
                if (next[1] == 'i') {
85 95c99430 Panagiotis Issaris
                    d*= pow( 2, e/0.3);
86
                    next+=2;
87 0cd28d94 Stefano Sabatini
                } else {
88 95c99430 Panagiotis Issaris
                    d*= pow(10, e);
89
                    next++;
90
                }
91
            }
92
        }
93
94 0cd28d94 Stefano Sabatini
        if (*next=='B') {
95 95c99430 Panagiotis Issaris
            d*=8;
96 a02142a5 Luca Barbato
            next++;
97 95c99430 Panagiotis Issaris
        }
98
    }
99
    /* if requested, fill in tail with the position after the last parsed
100
       character */
101
    if (tail)
102
        *tail = next;
103
    return d;
104
}
105
106 0cd28d94 Stefano Sabatini
static int strmatch(const char *s, const char *prefix)
107
{
108 3aa102be Michael Niedermayer
    int i;
109 0cd28d94 Stefano Sabatini
    for (i=0; prefix[i]; i++) {
110
        if (prefix[i] != s[i]) return 0;
111 3aa102be Michael Niedermayer
    }
112
    return 1;
113
}
114
115 a3731cad Stefano Sabatini
struct AVExpr {
116 85b4eb08 Oded Shimon
    enum {
117
        e_value, e_const, e_func0, e_func1, e_func2,
118 835954e3 Michael Niedermayer
        e_squish, e_gauss, e_ld,
119 85b4eb08 Oded Shimon
        e_mod, e_max, e_min, e_eq, e_gt, e_gte,
120
        e_pow, e_mul, e_div, e_add,
121 45ee0e32 Michael Niedermayer
        e_last, e_st, e_while,
122 85b4eb08 Oded Shimon
    } type;
123
    double value; // is sign in other types
124
    union {
125
        int const_index;
126
        double (*func0)(double);
127
        double (*func1)(void *, double);
128
        double (*func2)(void *, double, double);
129
    } a;
130 a3731cad Stefano Sabatini
    struct AVExpr *param[2];
131 85b4eb08 Oded Shimon
};
132
133 0cd28d94 Stefano Sabatini
static double eval_expr(Parser *p, AVExpr *e)
134
{
135 85b4eb08 Oded Shimon
    switch (e->type) {
136
        case e_value:  return e->value;
137 9711439b Stefano Sabatini
        case e_const:  return e->value * p->const_values[e->a.const_index];
138 85b4eb08 Oded Shimon
        case e_func0:  return e->value * e->a.func0(eval_expr(p, e->param[0]));
139
        case e_func1:  return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
140
        case e_func2:  return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
141
        case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
142
        case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
143 f66e4f5f Reimar Döffinger
        case e_ld:     return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
144 45ee0e32 Michael Niedermayer
        case e_while: {
145 63a547ea Oded Shimon
            double d = NAN;
146 0cd28d94 Stefano Sabatini
            while (eval_expr(p, e->param[0]))
147 45ee0e32 Michael Niedermayer
                d=eval_expr(p, e->param[1]);
148
            return d;
149
        }
150 85b4eb08 Oded Shimon
        default: {
151
            double d = eval_expr(p, e->param[0]);
152
            double d2 = eval_expr(p, e->param[1]);
153
            switch (e->type) {
154
                case e_mod: return e->value * (d - floor(d/d2)*d2);
155
                case e_max: return e->value * (d >  d2 ?   d : d2);
156
                case e_min: return e->value * (d <  d2 ?   d : d2);
157
                case e_eq:  return e->value * (d == d2 ? 1.0 : 0.0);
158
                case e_gt:  return e->value * (d >  d2 ? 1.0 : 0.0);
159
                case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
160
                case e_pow: return e->value * pow(d, d2);
161
                case e_mul: return e->value * (d * d2);
162
                case e_div: return e->value * (d / d2);
163
                case e_add: return e->value * (d + d2);
164 72523c7a Oded Shimon
                case e_last:return e->value * d2;
165 f66e4f5f Reimar Döffinger
                case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
166 85b4eb08 Oded Shimon
            }
167
        }
168
    }
169
    return NAN;
170
}
171
172 9ace13b4 Stefano Sabatini
static int parse_expr(AVExpr **e, Parser *p);
173 85b4eb08 Oded Shimon
174 0b99215c Stefano Sabatini
void av_free_expr(AVExpr *e)
175 0cd28d94 Stefano Sabatini
{
176 85b4eb08 Oded Shimon
    if (!e) return;
177 0b99215c Stefano Sabatini
    av_free_expr(e->param[0]);
178
    av_free_expr(e->param[1]);
179 85b4eb08 Oded Shimon
    av_freep(&e);
180
}
181
182 9ace13b4 Stefano Sabatini
static int parse_primary(AVExpr **e, Parser *p)
183
{
184 0cd28d94 Stefano Sabatini
    AVExpr *d = av_mallocz(sizeof(AVExpr));
185 2f62224b Stefano Sabatini
    char *next = p->s, *s0 = p->s;
186 9ace13b4 Stefano Sabatini
    int ret, i;
187 3aa102be Michael Niedermayer
188 0345f36b Ramiro Polla
    if (!d)
189 9ace13b4 Stefano Sabatini
        return AVERROR(ENOMEM);
190 0345f36b Ramiro Polla
191 3aa102be Michael Niedermayer
    /* number */
192 85b4eb08 Oded Shimon
    d->value = av_strtod(p->s, &next);
193 0cd28d94 Stefano Sabatini
    if (next != p->s) {
194 85b4eb08 Oded Shimon
        d->type = e_value;
195 3aa102be Michael Niedermayer
        p->s= next;
196 9ace13b4 Stefano Sabatini
        *e = d;
197
        return 0;
198 3aa102be Michael Niedermayer
    }
199 85b4eb08 Oded Shimon
    d->value = 1;
200 115329f1 Diego Biurrun
201 3aa102be Michael Niedermayer
    /* named constants */
202 0cd28d94 Stefano Sabatini
    for (i=0; p->const_names && p->const_names[i]; i++) {
203
        if (strmatch(p->s, p->const_names[i])) {
204 9711439b Stefano Sabatini
            p->s+= strlen(p->const_names[i]);
205 85b4eb08 Oded Shimon
            d->type = e_const;
206
            d->a.const_index = i;
207 9ace13b4 Stefano Sabatini
            *e = d;
208
            return 0;
209 3aa102be Michael Niedermayer
        }
210
    }
211 115329f1 Diego Biurrun
212 3aa102be Michael Niedermayer
    p->s= strchr(p->s, '(');
213 0cd28d94 Stefano Sabatini
    if (p->s==NULL) {
214 2f62224b Stefano Sabatini
        av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
215 e6b12001 Michael Niedermayer
        p->s= next;
216 0b99215c Stefano Sabatini
        av_free_expr(d);
217 9ace13b4 Stefano Sabatini
        return AVERROR(EINVAL);
218 3aa102be Michael Niedermayer
    }
219
    p->s++; // "("
220 85b4eb08 Oded Shimon
    if (*next == '(') { // special case do-nothing
221
        av_freep(&d);
222 9ace13b4 Stefano Sabatini
        if ((ret = parse_expr(&d, p)) < 0)
223
            return ret;
224 0cd28d94 Stefano Sabatini
        if (p->s[0] != ')') {
225 2f62224b Stefano Sabatini
            av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
226 0b99215c Stefano Sabatini
            av_free_expr(d);
227 9ace13b4 Stefano Sabatini
            return AVERROR(EINVAL);
228 85b4eb08 Oded Shimon
        }
229
        p->s++; // ")"
230 9ace13b4 Stefano Sabatini
        *e = d;
231
        return 0;
232
    }
233
    if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
234 0b99215c Stefano Sabatini
        av_free_expr(d);
235 9ace13b4 Stefano Sabatini
        return ret;
236 85b4eb08 Oded Shimon
    }
237 0cd28d94 Stefano Sabatini
    if (p->s[0]== ',') {
238 69f5de18 Alex Beregszaszi
        p->s++; // ","
239 9ace13b4 Stefano Sabatini
        parse_expr(&d->param[1], p);
240 3aa102be Michael Niedermayer
    }
241 0cd28d94 Stefano Sabatini
    if (p->s[0] != ')') {
242 2f62224b Stefano Sabatini
        av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
243 0b99215c Stefano Sabatini
        av_free_expr(d);
244 9ace13b4 Stefano Sabatini
        return AVERROR(EINVAL);
245 69f5de18 Alex Beregszaszi
    }
246
    p->s++; // ")"
247 115329f1 Diego Biurrun
248 85b4eb08 Oded Shimon
    d->type = e_func0;
249 0cd28d94 Stefano Sabatini
         if (strmatch(next, "sinh"  )) d->a.func0 = sinh;
250
    else if (strmatch(next, "cosh"  )) d->a.func0 = cosh;
251
    else if (strmatch(next, "tanh"  )) d->a.func0 = tanh;
252
    else if (strmatch(next, "sin"   )) d->a.func0 = sin;
253
    else if (strmatch(next, "cos"   )) d->a.func0 = cos;
254
    else if (strmatch(next, "tan"   )) d->a.func0 = tan;
255
    else if (strmatch(next, "atan"  )) d->a.func0 = atan;
256
    else if (strmatch(next, "asin"  )) d->a.func0 = asin;
257
    else if (strmatch(next, "acos"  )) d->a.func0 = acos;
258
    else if (strmatch(next, "exp"   )) d->a.func0 = exp;
259
    else if (strmatch(next, "log"   )) d->a.func0 = log;
260
    else if (strmatch(next, "abs"   )) d->a.func0 = fabs;
261
    else if (strmatch(next, "squish")) d->type = e_squish;
262
    else if (strmatch(next, "gauss" )) d->type = e_gauss;
263
    else if (strmatch(next, "mod"   )) d->type = e_mod;
264
    else if (strmatch(next, "max"   )) d->type = e_max;
265
    else if (strmatch(next, "min"   )) d->type = e_min;
266
    else if (strmatch(next, "eq"    )) d->type = e_eq;
267
    else if (strmatch(next, "gte"   )) d->type = e_gte;
268
    else if (strmatch(next, "gt"    )) d->type = e_gt;
269
    else if (strmatch(next, "lte"   )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
270
    else if (strmatch(next, "lt"    )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
271
    else if (strmatch(next, "ld"    )) d->type = e_ld;
272
    else if (strmatch(next, "st"    )) d->type = e_st;
273
    else if (strmatch(next, "while" )) d->type = e_while;
274 85b4eb08 Oded Shimon
    else {
275 0cd28d94 Stefano Sabatini
        for (i=0; p->func1_names && p->func1_names[i]; i++) {
276
            if (strmatch(next, p->func1_names[i])) {
277 9711439b Stefano Sabatini
                d->a.func1 = p->funcs1[i];
278 85b4eb08 Oded Shimon
                d->type = e_func1;
279 9ace13b4 Stefano Sabatini
                *e = d;
280
                return 0;
281 3aa102be Michael Niedermayer
            }
282
        }
283
284 0cd28d94 Stefano Sabatini
        for (i=0; p->func2_names && p->func2_names[i]; i++) {
285
            if (strmatch(next, p->func2_names[i])) {
286 9711439b Stefano Sabatini
                d->a.func2 = p->funcs2[i];
287 85b4eb08 Oded Shimon
                d->type = e_func2;
288 9ace13b4 Stefano Sabatini
                *e = d;
289
                return 0;
290 3aa102be Michael Niedermayer
            }
291
        }
292
293 2f62224b Stefano Sabatini
        av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
294 0b99215c Stefano Sabatini
        av_free_expr(d);
295 9ace13b4 Stefano Sabatini
        return AVERROR(EINVAL);
296 3aa102be Michael Niedermayer
    }
297 b349fde1 Michael Niedermayer
298 9ace13b4 Stefano Sabatini
    *e = d;
299
    return 0;
300 115329f1 Diego Biurrun
}
301 80a49958 Michael Niedermayer
302 0cd28d94 Stefano Sabatini
static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
303
{
304
    AVExpr *e = av_mallocz(sizeof(AVExpr));
305 0345f36b Ramiro Polla
    if (!e)
306
        return NULL;
307 a98f4515 Michael Niedermayer
    e->type     =type   ;
308
    e->value    =value  ;
309
    e->param[0] =p0     ;
310
    e->param[1] =p1     ;
311
    return e;
312
}
313
314 9ace13b4 Stefano Sabatini
static int parse_pow(AVExpr **e, Parser *p, int *sign)
315
{
316 2c409cc7 Michael Niedermayer
    *sign= (*p->s == '+') - (*p->s == '-');
317
    p->s += *sign&1;
318 9ace13b4 Stefano Sabatini
    return parse_primary(e, p);
319 3aa102be Michael Niedermayer
}
320
321 9ace13b4 Stefano Sabatini
static int parse_factor(AVExpr **e, Parser *p)
322
{
323
    int sign, sign2, ret;
324
    AVExpr *e0, *e1, *e2;
325
    if ((ret = parse_pow(&e0, p, &sign)) < 0)
326
        return ret;
327 3aa102be Michael Niedermayer
    while(p->s[0]=='^'){
328 9ace13b4 Stefano Sabatini
        e1 = e0;
329 3aa102be Michael Niedermayer
        p->s++;
330 9ace13b4 Stefano Sabatini
        if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
331 0b99215c Stefano Sabatini
            av_free_expr(e1);
332 9ace13b4 Stefano Sabatini
            return ret;
333
        }
334
        e0 = new_eval_expr(e_pow, 1, e1, e2);
335
        if (!e0) {
336 0b99215c Stefano Sabatini
            av_free_expr(e1);
337
            av_free_expr(e2);
338 9ace13b4 Stefano Sabatini
            return AVERROR(ENOMEM);
339
        }
340
        if (e0->param[1]) e0->param[1]->value *= (sign2|1);
341 3aa102be Michael Niedermayer
    }
342 9ace13b4 Stefano Sabatini
    if (e0) e0->value *= (sign|1);
343
344
    *e = e0;
345
    return 0;
346 3aa102be Michael Niedermayer
}
347
348 9ace13b4 Stefano Sabatini
static int parse_term(AVExpr **e, Parser *p)
349
{
350
    int ret;
351
    AVExpr *e0, *e1, *e2;
352
    if ((ret = parse_factor(&e0, p)) < 0)
353
        return ret;
354 0cd28d94 Stefano Sabatini
    while (p->s[0]=='*' || p->s[0]=='/') {
355 a98f4515 Michael Niedermayer
        int c= *p->s++;
356 9ace13b4 Stefano Sabatini
        e1 = e0;
357
        if ((ret = parse_factor(&e2, p)) < 0) {
358 0b99215c Stefano Sabatini
            av_free_expr(e1);
359 9ace13b4 Stefano Sabatini
            return ret;
360
        }
361
        e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
362
        if (!e0) {
363 0b99215c Stefano Sabatini
            av_free_expr(e1);
364
            av_free_expr(e2);
365 9ace13b4 Stefano Sabatini
            return AVERROR(ENOMEM);
366
        }
367 3aa102be Michael Niedermayer
    }
368 9ace13b4 Stefano Sabatini
    *e = e0;
369
    return 0;
370 3aa102be Michael Niedermayer
}
371
372 9ace13b4 Stefano Sabatini
static int parse_subexpr(AVExpr **e, Parser *p)
373
{
374
    int ret;
375
    AVExpr *e0, *e1, *e2;
376
    if ((ret = parse_term(&e0, p)) < 0)
377
        return ret;
378 0cd28d94 Stefano Sabatini
    while (*p->s == '+' || *p->s == '-') {
379 9ace13b4 Stefano Sabatini
        e1 = e0;
380
        if ((ret = parse_term(&e2, p)) < 0) {
381 0b99215c Stefano Sabatini
            av_free_expr(e1);
382 9ace13b4 Stefano Sabatini
            return ret;
383
        }
384
        e0 = new_eval_expr(e_add, 1, e1, e2);
385
        if (!e0) {
386 0b99215c Stefano Sabatini
            av_free_expr(e1);
387
            av_free_expr(e2);
388 9ace13b4 Stefano Sabatini
            return AVERROR(ENOMEM);
389
        }
390 835954e3 Michael Niedermayer
    };
391
392 9ace13b4 Stefano Sabatini
    *e = e0;
393
    return 0;
394 835954e3 Michael Niedermayer
}
395
396 9ace13b4 Stefano Sabatini
static int parse_expr(AVExpr **e, Parser *p)
397
{
398
    int ret;
399
    AVExpr *e0, *e1, *e2;
400 0cd28d94 Stefano Sabatini
    if (p->stack_index <= 0) //protect against stack overflows
401 9ace13b4 Stefano Sabatini
        return AVERROR(EINVAL);
402 67e11730 Michael Niedermayer
    p->stack_index--;
403
404 9ace13b4 Stefano Sabatini
    if ((ret = parse_subexpr(&e0, p)) < 0)
405
        return ret;
406 0cd28d94 Stefano Sabatini
    while (*p->s == ';') {
407 63b16303 Stefano Sabatini
        p->s++;
408 9ace13b4 Stefano Sabatini
        e1 = e0;
409
        if ((ret = parse_subexpr(&e2, p)) < 0) {
410 0b99215c Stefano Sabatini
            av_free_expr(e1);
411 9ace13b4 Stefano Sabatini
            return ret;
412
        }
413
        e0 = new_eval_expr(e_last, 1, e1, e2);
414
        if (!e0) {
415 0b99215c Stefano Sabatini
            av_free_expr(e1);
416
            av_free_expr(e2);
417 9ace13b4 Stefano Sabatini
            return AVERROR(ENOMEM);
418
        }
419 85b4eb08 Oded Shimon
    };
420 67e11730 Michael Niedermayer
421
    p->stack_index++;
422 9ace13b4 Stefano Sabatini
    *e = e0;
423
    return 0;
424 3aa102be Michael Niedermayer
}
425
426 0cd28d94 Stefano Sabatini
static int verify_expr(AVExpr *e)
427
{
428 85b4eb08 Oded Shimon
    if (!e) return 0;
429
    switch (e->type) {
430
        case e_value:
431
        case e_const: return 1;
432
        case e_func0:
433
        case e_func1:
434
        case e_squish:
435 835954e3 Michael Niedermayer
        case e_ld:
436 85b4eb08 Oded Shimon
        case e_gauss: return verify_expr(e->param[0]);
437
        default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
438
    }
439
}
440
441 0b99215c Stefano Sabatini
int av_parse_expr(AVExpr **expr, const char *s,
442 9711439b Stefano Sabatini
                  const char * const *const_names,
443
                  const char * const *func1_names, double (* const *funcs1)(void *, double),
444
                  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
445 b851dd9e Stefano Sabatini
                  int log_offset, void *log_ctx)
446 2b65bb45 Michael Niedermayer
{
447 3aa102be Michael Niedermayer
    Parser p;
448 073f6d5b Stefano Sabatini
    AVExpr *e = NULL;
449 0314dead Måns Rullgård
    char *w = av_malloc(strlen(s) + 1);
450
    char *wp = w;
451 7ec9b9be Stefano Sabatini
    const char *s0 = s;
452 9ace13b4 Stefano Sabatini
    int ret = 0;
453 0314dead Måns Rullgård
454
    if (!w)
455 9ace13b4 Stefano Sabatini
        return AVERROR(ENOMEM);
456 8cd68d80 Oded Shimon
457
    while (*s)
458
        if (!isspace(*s++)) *wp++ = s[-1];
459
    *wp++ = 0;
460 115329f1 Diego Biurrun
461 2b65bb45 Michael Niedermayer
    p.class      = &class;
462 67e11730 Michael Niedermayer
    p.stack_index=100;
463 8cd68d80 Oded Shimon
    p.s= w;
464 9711439b Stefano Sabatini
    p.const_names = const_names;
465
    p.funcs1      = funcs1;
466
    p.func1_names = func1_names;
467
    p.funcs2      = funcs2;
468
    p.func2_names = func2_names;
469 2b65bb45 Michael Niedermayer
    p.log_offset = log_offset;
470
    p.log_ctx    = log_ctx;
471 115329f1 Diego Biurrun
472 9ace13b4 Stefano Sabatini
    if ((ret = parse_expr(&e, &p)) < 0)
473
        goto end;
474 7ec9b9be Stefano Sabatini
    if (*p.s) {
475
        av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
476
        ret = AVERROR(EINVAL);
477
        goto end;
478
    }
479 85b4eb08 Oded Shimon
    if (!verify_expr(e)) {
480 0b99215c Stefano Sabatini
        av_free_expr(e);
481 9ace13b4 Stefano Sabatini
        ret = AVERROR(EINVAL);
482
        goto end;
483 85b4eb08 Oded Shimon
    }
484 9ace13b4 Stefano Sabatini
    *expr = e;
485 0314dead Måns Rullgård
end:
486
    av_free(w);
487 9ace13b4 Stefano Sabatini
    return ret;
488 85b4eb08 Oded Shimon
}
489
490 0b99215c Stefano Sabatini
double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
491 0cd28d94 Stefano Sabatini
{
492 85b4eb08 Oded Shimon
    Parser p;
493
494 9711439b Stefano Sabatini
    p.const_values = const_values;
495 85b4eb08 Oded Shimon
    p.opaque     = opaque;
496
    return eval_expr(&p, e);
497
}
498
499 0b99215c Stefano Sabatini
int av_parse_and_eval_expr(double *d, const char *s,
500 9711439b Stefano Sabatini
                           const char * const *const_names, const double *const_values,
501
                           const char * const *func1_names, double (* const *funcs1)(void *, double),
502
                           const char * const *func2_names, double (* const *funcs2)(void *, double, double),
503 b851dd9e Stefano Sabatini
                           void *opaque, int log_offset, void *log_ctx)
504 2b65bb45 Michael Niedermayer
{
505 9ace13b4 Stefano Sabatini
    AVExpr *e = NULL;
506 0b99215c Stefano Sabatini
    int ret = av_parse_expr(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
507 9ace13b4 Stefano Sabatini
508
    if (ret < 0) {
509
        *d = NAN;
510
        return ret;
511
    }
512 0b99215c Stefano Sabatini
    *d = av_eval_expr(e, const_values, opaque);
513
    av_free_expr(e);
514 9ace13b4 Stefano Sabatini
    return isnan(*d) ? AVERROR(EINVAL) : 0;
515 3aa102be Michael Niedermayer
}
516 b349fde1 Michael Niedermayer
517
#ifdef TEST
518 115329f1 Diego Biurrun
#undef printf
519 0cd28d94 Stefano Sabatini
static double const_values[] = {
520 b349fde1 Michael Niedermayer
    M_PI,
521
    M_E,
522
    0
523
};
524 b851dd9e Stefano Sabatini
525 0cd28d94 Stefano Sabatini
static const char *const_names[] = {
526 b349fde1 Michael Niedermayer
    "PI",
527
    "E",
528
    0
529
};
530 b851dd9e Stefano Sabatini
531 0cd28d94 Stefano Sabatini
int main(void)
532
{
533 80a49958 Michael Niedermayer
    int i;
534 9ace13b4 Stefano Sabatini
    double d;
535 eb928286 Stefano Sabatini
    const char **expr, *exprs[] = {
536
        "",
537 63b16303 Stefano Sabatini
        "1;2",
538 8bb12e71 Stefano Sabatini
        "-20",
539
        "-PI",
540
        "+PI",
541 eb928286 Stefano Sabatini
        "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
542
        "80G/80Gi"
543
        "1k",
544
        "1Gi",
545
        "1gi",
546
        "1GiFoo",
547
        "1k+1k",
548
        "1Gi*3foo",
549
        "foo",
550
        "foo(",
551
        "foo()",
552
        "foo)",
553
        "sin",
554
        "sin(",
555
        "sin()",
556
        "sin)",
557
        "sin 10",
558
        "sin(1,2,3)",
559
        "sin(1 )",
560
        "1",
561
        "1foo",
562
        "bar + PI + E + 100f*2 + foo",
563
        "13k + 12f - foo(1, 2)",
564
        "1gi",
565
        "1Gi",
566 2b59fbe9 Stefano Sabatini
        "st(0, 123)",
567
        "st(1, 123); ld(1)",
568
        /* compute 1+2+...+N */
569
        "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
570
        /* compute Fib(N) */
571
        "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)",
572
        "while(0, 10)",
573
        "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
574 eb928286 Stefano Sabatini
        NULL
575
    };
576
577
    for (expr = exprs; *expr; expr++) {
578
        printf("Evaluating '%s'\n", *expr);
579
        av_parse_and_eval_expr(&d, *expr,
580
                               const_names, const_values,
581
                               NULL, NULL, NULL, NULL, NULL, 0, NULL);
582
        printf("'%s' -> %f\n\n", *expr, d);
583
    }
584
585 0b99215c Stefano Sabatini
    av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
586 b851dd9e Stefano Sabatini
                           const_names, const_values,
587
                           NULL, NULL, NULL, NULL, NULL, 0, NULL);
588 9ace13b4 Stefano Sabatini
    printf("%f == 12.7\n", d);
589 0b99215c Stefano Sabatini
    av_parse_and_eval_expr(&d, "80G/80Gi",
590 b851dd9e Stefano Sabatini
                           const_names, const_values,
591 79513857 Stefano Sabatini
                           NULL, NULL, NULL, NULL, NULL, 0, NULL);
592 9ace13b4 Stefano Sabatini
    printf("%f == 0.931322575\n", d);
593 115329f1 Diego Biurrun
594 0cd28d94 Stefano Sabatini
    for (i=0; i<1050; i++) {
595 80a49958 Michael Niedermayer
        START_TIMER
596 0b99215c Stefano Sabatini
            av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
597 b851dd9e Stefano Sabatini
                                   const_names, const_values,
598
                                   NULL, NULL, NULL, NULL, NULL, 0, NULL);
599 0b99215c Stefano Sabatini
        STOP_TIMER("av_parse_and_eval_expr")
600 80a49958 Michael Niedermayer
    }
601 e5b10e31 Diego Biurrun
    return 0;
602 b349fde1 Michael Niedermayer
}
603
#endif