annotate eval.c @ 947:34a4ee230079 libavutil

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