Mercurial > libavcodec.hg
annotate eval.c @ 11738:9bfef228a117 libavcodec
Factorize code into a single function.
Patch by Sebastian Vater, cdgs D basty A gmail
author | cehoyos |
---|---|
date | Sun, 16 May 2010 21:03:08 +0000 |
parents | 7dd2a45249a9 |
children | ceffa0ca7596 |
rev | line source |
---|---|
612 | 1 /* |
4101 | 2 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at> |
4099
5e5c34470242
I hope noone minds, adding myself to eval.c copyright...
ods15
parents:
4095
diff
changeset
|
3 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org> |
612 | 4 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3920
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3920
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3920
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
612 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3920
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
612 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3920
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
612 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3920
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
612 | 20 */ |
21 | |
1106 | 22 /** |
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11616
diff
changeset
|
23 * @file |
1106 | 24 * simple arithmetic expression evaluator. |
25 * | |
612 | 26 * see http://joe.hotchkiss.com/programming/eval/eval.html |
27 */ | |
28 | |
11613 | 29 #include "libavutil/avutil.h" |
10039 | 30 #include "eval.h" |
31 | |
612 | 32 typedef struct Parser{ |
33 int stack_index; | |
34 char *s; | |
8320 | 35 const double *const_value; |
36 const char * const *const_name; // NULL terminated | |
11616
1461e6044153
Fix constness for func[12] parameters in ff_parse_expr() and
stefano
parents:
11615
diff
changeset
|
37 double (* const *func1)(void *, double a); // NULL terminated |
11615
17ce5438a9c9
Change constness for func[12]_name parameters of ff_parse_expr() and
stefano
parents:
11614
diff
changeset
|
38 const char * const *func1_name; // NULL terminated |
11616
1461e6044153
Fix constness for func[12] parameters in ff_parse_expr() and
stefano
parents:
11615
diff
changeset
|
39 double (* const *func2)(void *, double a, double b); // NULL terminated |
11615
17ce5438a9c9
Change constness for func[12]_name parameters of ff_parse_expr() and
stefano
parents:
11614
diff
changeset
|
40 const char * const *func2_name; // NULL terminated |
612 | 41 void *opaque; |
6324 | 42 const char **error; |
4089 | 43 #define VARS 10 |
44 double var[VARS]; | |
612 | 45 } Parser; |
46 | |
7129 | 47 static const int8_t si_prefixes['z' - 'E' + 1]={ |
3778 | 48 ['y'-'E']= -24, |
49 ['z'-'E']= -21, | |
50 ['a'-'E']= -18, | |
51 ['f'-'E']= -15, | |
52 ['p'-'E']= -12, | |
53 ['n'-'E']= - 9, | |
54 ['u'-'E']= - 6, | |
55 ['m'-'E']= - 3, | |
56 ['c'-'E']= - 2, | |
57 ['d'-'E']= - 1, | |
58 ['h'-'E']= 2, | |
59 ['k'-'E']= 3, | |
60 ['K'-'E']= 3, | |
61 ['M'-'E']= 6, | |
62 ['G'-'E']= 9, | |
63 ['T'-'E']= 12, | |
64 ['P'-'E']= 15, | |
65 ['E'-'E']= 18, | |
66 ['Z'-'E']= 21, | |
67 ['Y'-'E']= 24, | |
68 }; | |
3756 | 69 |
9880 | 70 double av_strtod(const char *numstr, char **tail) { |
3778 | 71 double d; |
72 char *next; | |
9879
d54ba41c7e48
Cosmetics: rename 'name' av_strtod() param to 'numstr'. The new name
stefano
parents:
8718
diff
changeset
|
73 d = strtod(numstr, &next); |
3778 | 74 /* if parsing succeeded, check for and interpret postfixes */ |
9879
d54ba41c7e48
Cosmetics: rename 'name' av_strtod() param to 'numstr'. The new name
stefano
parents:
8718
diff
changeset
|
75 if (next!=numstr) { |
3778 | 76 |
77 if(*next >= 'E' && *next <= 'z'){ | |
78 int e= si_prefixes[*next - 'E']; | |
79 if(e){ | |
80 if(next[1] == 'i'){ | |
81 d*= pow( 2, e/0.3); | |
82 next+=2; | |
83 }else{ | |
84 d*= pow(10, e); | |
85 next++; | |
86 } | |
87 } | |
88 } | |
89 | |
90 if(*next=='B') { | |
91 d*=8; | |
4355 | 92 next++; |
3778 | 93 } |
94 } | |
95 /* if requested, fill in tail with the position after the last parsed | |
96 character */ | |
97 if (tail) | |
98 *tail = next; | |
99 return d; | |
100 } | |
612 | 101 |
1057 | 102 static int strmatch(const char *s, const char *prefix){ |
612 | 103 int i; |
104 for(i=0; prefix[i]; i++){ | |
105 if(prefix[i] != s[i]) return 0; | |
106 } | |
107 return 1; | |
108 } | |
109 | |
11601
29fda2500178
Avoid the use of the symbol ff_expr_s for referencing AVExpr.
stefano
parents:
11597
diff
changeset
|
110 struct AVExpr { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
111 enum { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
112 e_value, e_const, e_func0, e_func1, e_func2, |
4089 | 113 e_squish, e_gauss, e_ld, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
114 e_mod, e_max, e_min, e_eq, e_gt, e_gte, |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
115 e_pow, e_mul, e_div, e_add, |
4090
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
116 e_last, e_st, e_while, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
117 } type; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
118 double value; // is sign in other types |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
119 union { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
120 int const_index; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
121 double (*func0)(double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
122 double (*func1)(void *, double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
123 double (*func2)(void *, double, double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
124 } a; |
11601
29fda2500178
Avoid the use of the symbol ff_expr_s for referencing AVExpr.
stefano
parents:
11597
diff
changeset
|
125 struct AVExpr *param[2]; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
126 }; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
127 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
128 static double eval_expr(Parser * p, AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
129 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
130 case e_value: return e->value; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
131 case e_const: return e->value * p->const_value[e->a.const_index]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
132 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0])); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
133 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0])); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
134 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1])); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
135 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0]))); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
136 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); } |
4594 | 137 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)]; |
4090
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
138 case e_while: { |
4092
772ab2a1deaa
shut gcc warning, also makes sense for NAN to be returned if the loop was never executed
ods15
parents:
4090
diff
changeset
|
139 double d = NAN; |
4090
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
140 while(eval_expr(p, e->param[0])) |
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
141 d=eval_expr(p, e->param[1]); |
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
142 return d; |
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
143 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
144 default: { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
145 double d = eval_expr(p, e->param[0]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
146 double d2 = eval_expr(p, e->param[1]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
147 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
148 case e_mod: return e->value * (d - floor(d/d2)*d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
149 case e_max: return e->value * (d > d2 ? d : d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
150 case e_min: return e->value * (d < d2 ? d : d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
151 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
152 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
153 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
154 case e_pow: return e->value * pow(d, d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
155 case e_mul: return e->value * (d * d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
156 case e_div: return e->value * (d / d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
157 case e_add: return e->value * (d + d2); |
4093 | 158 case e_last:return e->value * d2; |
4594 | 159 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
160 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
161 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
162 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
163 return NAN; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
164 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
165 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
166 static AVExpr * parse_expr(Parser *p); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
167 |
11597 | 168 void ff_free_expr(AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
169 if (!e) return; |
11597 | 170 ff_free_expr(e->param[0]); |
171 ff_free_expr(e->param[1]); | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
172 av_freep(&e); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
173 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
174 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
175 static AVExpr * parse_primary(Parser *p) { |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
176 AVExpr * d = av_mallocz(sizeof(AVExpr)); |
612 | 177 char *next= p->s; |
178 int i; | |
179 | |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
180 if (!d) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
181 return NULL; |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
182 |
612 | 183 /* number */ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
184 d->value = av_strtod(p->s, &next); |
612 | 185 if(next != p->s){ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
186 d->type = e_value; |
612 | 187 p->s= next; |
2434 | 188 return d; |
612 | 189 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
190 d->value = 1; |
2967 | 191 |
612 | 192 /* named constants */ |
2433 | 193 for(i=0; p->const_name && p->const_name[i]; i++){ |
612 | 194 if(strmatch(p->s, p->const_name[i])){ |
195 p->s+= strlen(p->const_name[i]); | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
196 d->type = e_const; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
197 d->a.const_index = i; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
198 return d; |
612 | 199 } |
200 } | |
2967 | 201 |
612 | 202 p->s= strchr(p->s, '('); |
203 if(p->s==NULL){ | |
6840 | 204 *p->error = "undefined constant or missing ("; |
3754 | 205 p->s= next; |
11597 | 206 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
207 return NULL; |
612 | 208 } |
209 p->s++; // "(" | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
210 if (*next == '(') { // special case do-nothing |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
211 av_freep(&d); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
212 d = parse_expr(p); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
213 if(p->s[0] != ')'){ |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
214 *p->error = "missing )"; |
11597 | 215 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
216 return NULL; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
217 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
218 p->s++; // ")" |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
219 return d; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
220 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
221 d->param[0] = parse_expr(p); |
1815 | 222 if(p->s[0]== ','){ |
223 p->s++; // "," | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
224 d->param[1] = parse_expr(p); |
612 | 225 } |
1815 | 226 if(p->s[0] != ')'){ |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
227 *p->error = "missing )"; |
11597 | 228 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
229 return NULL; |
1815 | 230 } |
231 p->s++; // ")" | |
2967 | 232 |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
233 d->type = e_func0; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
234 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
235 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
236 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
237 else if( strmatch(next, "sin" ) ) d->a.func0 = sin; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
238 else if( strmatch(next, "cos" ) ) d->a.func0 = cos; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
239 else if( strmatch(next, "tan" ) ) d->a.func0 = tan; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
240 else if( strmatch(next, "atan" ) ) d->a.func0 = atan; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
241 else if( strmatch(next, "asin" ) ) d->a.func0 = asin; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
242 else if( strmatch(next, "acos" ) ) d->a.func0 = acos; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
243 else if( strmatch(next, "exp" ) ) d->a.func0 = exp; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
244 else if( strmatch(next, "log" ) ) d->a.func0 = log; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
245 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
246 else if( strmatch(next, "squish") ) d->type = e_squish; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
247 else if( strmatch(next, "gauss" ) ) d->type = e_gauss; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
248 else if( strmatch(next, "mod" ) ) d->type = e_mod; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
249 else if( strmatch(next, "max" ) ) d->type = e_max; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
250 else if( strmatch(next, "min" ) ) d->type = e_min; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
251 else if( strmatch(next, "eq" ) ) d->type = e_eq; |
4087
d4cdb9f6e888
possible bug of 'gte' being read as 'gt', same with 'lte'
ods15
parents:
4086
diff
changeset
|
252 else if( strmatch(next, "gte" ) ) d->type = e_gte; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
253 else if( strmatch(next, "gt" ) ) d->type = e_gt; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
254 else if( strmatch(next, "lte" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; } |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
255 else if( strmatch(next, "lt" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; } |
4089 | 256 else if( strmatch(next, "ld" ) ) d->type = e_ld; |
257 else if( strmatch(next, "st" ) ) d->type = e_st; | |
4090
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
258 else if( strmatch(next, "while" ) ) d->type = e_while; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
259 else { |
612 | 260 for(i=0; p->func1_name && p->func1_name[i]; i++){ |
261 if(strmatch(next, p->func1_name[i])){ | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
262 d->a.func1 = p->func1[i]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
263 d->type = e_func1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
264 return d; |
612 | 265 } |
266 } | |
267 | |
268 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
269 if(strmatch(next, p->func2_name[i])){ | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
270 d->a.func2 = p->func2[i]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
271 d->type = e_func2; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
272 return d; |
612 | 273 } |
274 } | |
275 | |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
276 *p->error = "unknown function"; |
11597 | 277 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
278 return NULL; |
612 | 279 } |
2433 | 280 |
2434 | 281 return d; |
2967 | 282 } |
2436 | 283 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
284 static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1){ |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
285 AVExpr * e = av_mallocz(sizeof(AVExpr)); |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
286 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
287 return NULL; |
4085 | 288 e->type =type ; |
289 e->value =value ; | |
290 e->param[0] =p0 ; | |
291 e->param[1] =p1 ; | |
292 return e; | |
293 } | |
294 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
295 static AVExpr * parse_pow(Parser *p, int *sign){ |
4032 | 296 *sign= (*p->s == '+') - (*p->s == '-'); |
297 p->s += *sign&1; | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
298 return parse_primary(p); |
2434 | 299 } |
612 | 300 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
301 static AVExpr * parse_factor(Parser *p){ |
4032 | 302 int sign, sign2; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
303 AVExpr * e = parse_pow(p, &sign); |
2434 | 304 while(p->s[0]=='^'){ |
612 | 305 p->s++; |
4086 | 306 e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2)); |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
307 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
308 return NULL; |
4086 | 309 if (e->param[1]) e->param[1]->value *= (sign2|1); |
612 | 310 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
311 if (e) e->value *= (sign|1); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
312 return e; |
612 | 313 } |
314 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
315 static AVExpr * parse_term(Parser *p){ |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
316 AVExpr * e = parse_factor(p); |
2434 | 317 while(p->s[0]=='*' || p->s[0]=='/'){ |
4085 | 318 int c= *p->s++; |
319 e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p)); | |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
320 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
321 return NULL; |
612 | 322 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
323 return e; |
612 | 324 } |
325 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
326 static AVExpr * parse_subexpr(Parser *p) { |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
327 AVExpr * e = parse_term(p); |
4089 | 328 while(*p->s == '+' || *p->s == '-') { |
329 e= new_eval_expr(e_add, 1, e, parse_term(p)); | |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
330 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
331 return NULL; |
4089 | 332 }; |
333 | |
334 return e; | |
335 } | |
336 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
337 static AVExpr * parse_expr(Parser *p) { |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
338 AVExpr * e; |
612 | 339 |
2434 | 340 if(p->stack_index <= 0) //protect against stack overflows |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
341 return NULL; |
2434 | 342 p->stack_index--; |
343 | |
4089 | 344 e = parse_subexpr(p); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
345 |
4089 | 346 while(*p->s == ';') { |
347 p->s++; | |
348 e= new_eval_expr(e_last, 1, e, parse_subexpr(p)); | |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
349 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
350 return NULL; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
351 }; |
612 | 352 |
2434 | 353 p->stack_index++; |
612 | 354 |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
355 return e; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
356 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
357 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
358 static int verify_expr(AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
359 if (!e) return 0; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
360 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
361 case e_value: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
362 case e_const: return 1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
363 case e_func0: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
364 case e_func1: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
365 case e_squish: |
4089 | 366 case e_ld: |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
367 case e_gauss: return verify_expr(e->param[0]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
368 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
369 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
370 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
371 |
11614 | 372 AVExpr *ff_parse_expr(const char *s, const char * const *const_name, |
11616
1461e6044153
Fix constness for func[12] parameters in ff_parse_expr() and
stefano
parents:
11615
diff
changeset
|
373 double (* const *func1)(void *, double), const char * const *func1_name, |
1461e6044153
Fix constness for func[12] parameters in ff_parse_expr() and
stefano
parents:
11615
diff
changeset
|
374 double (* const *func2)(void *, double, double), const char * const *func2_name, |
6324 | 375 const char **error){ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
376 Parser p; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
377 AVExpr *e = NULL; |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
378 char *w = av_malloc(strlen(s) + 1); |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
379 char *wp = w; |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
380 |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
381 if (!w) |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
382 goto end; |
4095 | 383 |
384 while (*s) | |
385 if (!isspace(*s++)) *wp++ = s[-1]; | |
386 *wp++ = 0; | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
387 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
388 p.stack_index=100; |
4095 | 389 p.s= w; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
390 p.const_name = const_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
391 p.func1 = func1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
392 p.func1_name = func1_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
393 p.func2 = func2; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
394 p.func2_name = func2_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
395 p.error= error; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
396 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
397 e = parse_expr(&p); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
398 if (!verify_expr(e)) { |
11597 | 399 ff_free_expr(e); |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
400 e = NULL; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
401 } |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
402 end: |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
403 av_free(w); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
404 return e; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
405 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
406 |
11604 | 407 double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
408 Parser p; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
409 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
410 p.const_value= const_value; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
411 p.opaque = opaque; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
412 return eval_expr(&p, e); |
612 | 413 } |
414 | |
11605 | 415 double ff_parse_and_eval_expr(const char *s, const double *const_value, const char * const *const_name, |
11616
1461e6044153
Fix constness for func[12] parameters in ff_parse_expr() and
stefano
parents:
11615
diff
changeset
|
416 double (* const *func1)(void *, double), const char * const *func1_name, |
1461e6044153
Fix constness for func[12] parameters in ff_parse_expr() and
stefano
parents:
11615
diff
changeset
|
417 double (* const *func2)(void *, double, double), const char * const *func2_name, |
6324 | 418 void *opaque, const char **error){ |
11614 | 419 AVExpr * e = ff_parse_expr(s, const_name, func1, func1_name, func2, func2_name, error); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
420 double d; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
421 if (!e) return NAN; |
11604 | 422 d = ff_eval_expr(e, const_value, opaque); |
11597 | 423 ff_free_expr(e); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
424 return d; |
612 | 425 } |
2433 | 426 |
427 #ifdef TEST | |
2967 | 428 #undef printf |
2433 | 429 static double const_values[]={ |
430 M_PI, | |
431 M_E, | |
432 0 | |
433 }; | |
434 static const char *const_names[]={ | |
435 "PI", | |
436 "E", | |
437 0 | |
438 }; | |
6167 | 439 int main(void){ |
2436 | 440 int i; |
11605 | 441 printf("%f == 12.7\n", ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL)); |
442 printf("%f == 0.931322575\n", ff_parse_and_eval_expr("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL)); | |
2967 | 443 |
2436 | 444 for(i=0; i<1050; i++){ |
445 START_TIMER | |
11605 | 446 ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL); |
447 STOP_TIMER("ff_parse_and_eval_expr") | |
2436 | 448 } |
6167 | 449 return 0; |
2433 | 450 } |
451 #endif |