Mercurial > libavcodec.hg
annotate eval.c @ 11612:90b6d031c821 libavcodec
Extradata length checks for Huffyuv.
Patch by Michael Kaufmann hallo $(name) dash $(surname) ch
author | benoit |
---|---|
date | Mon, 12 Apr 2010 16:16:43 +0000 |
parents | 414a7cdaa54d |
children | 2c41284050cf |
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 /** |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8320
diff
changeset
|
23 * @file libavcodec/eval.c |
1106 | 24 * simple arithmetic expression evaluator. |
25 * | |
612 | 26 * see http://joe.hotchkiss.com/programming/eval/eval.html |
27 */ | |
28 | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
31 #include <string.h> | |
32 #include <math.h> | |
33 | |
10040
8f08abb1dffa
eval: include libavutil/mathematics.h for NAN and M_PI
mru
parents:
10039
diff
changeset
|
34 #include "libavutil/mathematics.h" |
10039 | 35 #include "avcodec.h" |
36 #include "eval.h" | |
37 | |
612 | 38 typedef struct Parser{ |
39 int stack_index; | |
40 char *s; | |
8320 | 41 const double *const_value; |
42 const char * const *const_name; // NULL terminated | |
612 | 43 double (**func1)(void *, double a); // NULL terminated |
1057 | 44 const char **func1_name; // NULL terminated |
612 | 45 double (**func2)(void *, double a, double b); // NULL terminated |
8320 | 46 const char **func2_name; // NULL terminated |
612 | 47 void *opaque; |
6324 | 48 const char **error; |
4089 | 49 #define VARS 10 |
50 double var[VARS]; | |
612 | 51 } Parser; |
52 | |
7129 | 53 static const int8_t si_prefixes['z' - 'E' + 1]={ |
3778 | 54 ['y'-'E']= -24, |
55 ['z'-'E']= -21, | |
56 ['a'-'E']= -18, | |
57 ['f'-'E']= -15, | |
58 ['p'-'E']= -12, | |
59 ['n'-'E']= - 9, | |
60 ['u'-'E']= - 6, | |
61 ['m'-'E']= - 3, | |
62 ['c'-'E']= - 2, | |
63 ['d'-'E']= - 1, | |
64 ['h'-'E']= 2, | |
65 ['k'-'E']= 3, | |
66 ['K'-'E']= 3, | |
67 ['M'-'E']= 6, | |
68 ['G'-'E']= 9, | |
69 ['T'-'E']= 12, | |
70 ['P'-'E']= 15, | |
71 ['E'-'E']= 18, | |
72 ['Z'-'E']= 21, | |
73 ['Y'-'E']= 24, | |
74 }; | |
3756 | 75 |
9880 | 76 double av_strtod(const char *numstr, char **tail) { |
3778 | 77 double d; |
78 char *next; | |
9879
d54ba41c7e48
Cosmetics: rename 'name' av_strtod() param to 'numstr'. The new name
stefano
parents:
8718
diff
changeset
|
79 d = strtod(numstr, &next); |
3778 | 80 /* 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
|
81 if (next!=numstr) { |
3778 | 82 |
83 if(*next >= 'E' && *next <= 'z'){ | |
84 int e= si_prefixes[*next - 'E']; | |
85 if(e){ | |
86 if(next[1] == 'i'){ | |
87 d*= pow( 2, e/0.3); | |
88 next+=2; | |
89 }else{ | |
90 d*= pow(10, e); | |
91 next++; | |
92 } | |
93 } | |
94 } | |
95 | |
96 if(*next=='B') { | |
97 d*=8; | |
4355 | 98 next++; |
3778 | 99 } |
100 } | |
101 /* if requested, fill in tail with the position after the last parsed | |
102 character */ | |
103 if (tail) | |
104 *tail = next; | |
105 return d; | |
106 } | |
612 | 107 |
1057 | 108 static int strmatch(const char *s, const char *prefix){ |
612 | 109 int i; |
110 for(i=0; prefix[i]; i++){ | |
111 if(prefix[i] != s[i]) return 0; | |
112 } | |
113 return 1; | |
114 } | |
115 | |
11601
29fda2500178
Avoid the use of the symbol ff_expr_s for referencing AVExpr.
stefano
parents:
11597
diff
changeset
|
116 struct AVExpr { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
117 enum { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
118 e_value, e_const, e_func0, e_func1, e_func2, |
4089 | 119 e_squish, e_gauss, e_ld, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
120 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
|
121 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
|
122 e_last, e_st, e_while, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
123 } type; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
124 double value; // is sign in other types |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
125 union { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
126 int const_index; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
127 double (*func0)(double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
128 double (*func1)(void *, double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
129 double (*func2)(void *, double, double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
130 } a; |
11601
29fda2500178
Avoid the use of the symbol ff_expr_s for referencing AVExpr.
stefano
parents:
11597
diff
changeset
|
131 struct AVExpr *param[2]; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
132 }; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
133 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
134 static double eval_expr(Parser * p, AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
135 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
136 case e_value: return e->value; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); } |
4594 | 143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 return d; |
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
149 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
150 default: { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
151 double d = eval_expr(p, e->param[0]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
152 double d2 = eval_expr(p, e->param[1]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
153 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 case e_pow: return e->value * pow(d, d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
161 case e_mul: return e->value * (d * d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
162 case e_div: return e->value * (d / d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
163 case e_add: return e->value * (d + d2); |
4093 | 164 case e_last:return e->value * d2; |
4594 | 165 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
|
166 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
167 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
168 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
169 return NAN; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
170 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
171 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
172 static AVExpr * parse_expr(Parser *p); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
173 |
11597 | 174 void ff_free_expr(AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
175 if (!e) return; |
11597 | 176 ff_free_expr(e->param[0]); |
177 ff_free_expr(e->param[1]); | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
178 av_freep(&e); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
179 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
180 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
181 static AVExpr * parse_primary(Parser *p) { |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
182 AVExpr * d = av_mallocz(sizeof(AVExpr)); |
612 | 183 char *next= p->s; |
184 int i; | |
185 | |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
186 if (!d) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
187 return NULL; |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
188 |
612 | 189 /* number */ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
190 d->value = av_strtod(p->s, &next); |
612 | 191 if(next != p->s){ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
192 d->type = e_value; |
612 | 193 p->s= next; |
2434 | 194 return d; |
612 | 195 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
196 d->value = 1; |
2967 | 197 |
612 | 198 /* named constants */ |
2433 | 199 for(i=0; p->const_name && p->const_name[i]; i++){ |
612 | 200 if(strmatch(p->s, p->const_name[i])){ |
201 p->s+= strlen(p->const_name[i]); | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
202 d->type = e_const; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
203 d->a.const_index = i; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
204 return d; |
612 | 205 } |
206 } | |
2967 | 207 |
612 | 208 p->s= strchr(p->s, '('); |
209 if(p->s==NULL){ | |
6840 | 210 *p->error = "undefined constant or missing ("; |
3754 | 211 p->s= next; |
11597 | 212 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
213 return NULL; |
612 | 214 } |
215 p->s++; // "(" | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
216 if (*next == '(') { // special case do-nothing |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
217 av_freep(&d); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
218 d = parse_expr(p); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
219 if(p->s[0] != ')'){ |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
220 *p->error = "missing )"; |
11597 | 221 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
222 return NULL; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
223 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
224 p->s++; // ")" |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
225 return d; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
226 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
227 d->param[0] = parse_expr(p); |
1815 | 228 if(p->s[0]== ','){ |
229 p->s++; // "," | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
230 d->param[1] = parse_expr(p); |
612 | 231 } |
1815 | 232 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
|
233 *p->error = "missing )"; |
11597 | 234 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
235 return NULL; |
1815 | 236 } |
237 p->s++; // ")" | |
2967 | 238 |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
239 d->type = e_func0; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
240 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
241 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
242 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
243 else if( strmatch(next, "sin" ) ) d->a.func0 = sin; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
244 else if( strmatch(next, "cos" ) ) d->a.func0 = cos; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
245 else if( strmatch(next, "tan" ) ) d->a.func0 = tan; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
246 else if( strmatch(next, "atan" ) ) d->a.func0 = atan; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
247 else if( strmatch(next, "asin" ) ) d->a.func0 = asin; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
248 else if( strmatch(next, "acos" ) ) d->a.func0 = acos; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
249 else if( strmatch(next, "exp" ) ) d->a.func0 = exp; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
250 else if( strmatch(next, "log" ) ) d->a.func0 = log; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
251 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
252 else if( strmatch(next, "squish") ) d->type = e_squish; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
253 else if( strmatch(next, "gauss" ) ) d->type = e_gauss; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
254 else if( strmatch(next, "mod" ) ) d->type = e_mod; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
255 else if( strmatch(next, "max" ) ) d->type = e_max; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
256 else if( strmatch(next, "min" ) ) d->type = e_min; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
257 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
|
258 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
|
259 else if( strmatch(next, "gt" ) ) d->type = e_gt; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
260 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
|
261 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 | 262 else if( strmatch(next, "ld" ) ) d->type = e_ld; |
263 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
|
264 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
|
265 else { |
612 | 266 for(i=0; p->func1_name && p->func1_name[i]; i++){ |
267 if(strmatch(next, p->func1_name[i])){ | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
268 d->a.func1 = p->func1[i]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
269 d->type = e_func1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
270 return d; |
612 | 271 } |
272 } | |
273 | |
274 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
275 if(strmatch(next, p->func2_name[i])){ | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
276 d->a.func2 = p->func2[i]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
277 d->type = e_func2; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
278 return d; |
612 | 279 } |
280 } | |
281 | |
3770
ea345e1e440f
Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents:
3756
diff
changeset
|
282 *p->error = "unknown function"; |
11597 | 283 ff_free_expr(d); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
284 return NULL; |
612 | 285 } |
2433 | 286 |
2434 | 287 return d; |
2967 | 288 } |
2436 | 289 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
290 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
|
291 AVExpr * e = av_mallocz(sizeof(AVExpr)); |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
292 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
293 return NULL; |
4085 | 294 e->type =type ; |
295 e->value =value ; | |
296 e->param[0] =p0 ; | |
297 e->param[1] =p1 ; | |
298 return e; | |
299 } | |
300 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
301 static AVExpr * parse_pow(Parser *p, int *sign){ |
4032 | 302 *sign= (*p->s == '+') - (*p->s == '-'); |
303 p->s += *sign&1; | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
304 return parse_primary(p); |
2434 | 305 } |
612 | 306 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
307 static AVExpr * parse_factor(Parser *p){ |
4032 | 308 int sign, sign2; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
309 AVExpr * e = parse_pow(p, &sign); |
2434 | 310 while(p->s[0]=='^'){ |
612 | 311 p->s++; |
4086 | 312 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
|
313 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
314 return NULL; |
4086 | 315 if (e->param[1]) e->param[1]->value *= (sign2|1); |
612 | 316 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
317 if (e) e->value *= (sign|1); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
318 return e; |
612 | 319 } |
320 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
321 static AVExpr * parse_term(Parser *p){ |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
322 AVExpr * e = parse_factor(p); |
2434 | 323 while(p->s[0]=='*' || p->s[0]=='/'){ |
4085 | 324 int c= *p->s++; |
325 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
|
326 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
327 return NULL; |
612 | 328 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
329 return e; |
612 | 330 } |
331 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
332 static AVExpr * parse_subexpr(Parser *p) { |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
333 AVExpr * e = parse_term(p); |
4089 | 334 while(*p->s == '+' || *p->s == '-') { |
335 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
|
336 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
337 return NULL; |
4089 | 338 }; |
339 | |
340 return e; | |
341 } | |
342 | |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
343 static AVExpr * parse_expr(Parser *p) { |
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
344 AVExpr * e; |
612 | 345 |
2434 | 346 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
|
347 return NULL; |
2434 | 348 p->stack_index--; |
349 | |
4089 | 350 e = parse_subexpr(p); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
351 |
4089 | 352 while(*p->s == ';') { |
353 p->s++; | |
354 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
|
355 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
356 return NULL; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
357 }; |
612 | 358 |
2434 | 359 p->stack_index++; |
612 | 360 |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
361 return e; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
362 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
363 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
364 static int verify_expr(AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
365 if (!e) return 0; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
366 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
367 case e_value: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
368 case e_const: return 1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
369 case e_func0: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
370 case e_func1: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
371 case e_squish: |
4089 | 372 case e_ld: |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
373 case e_gauss: return verify_expr(e->param[0]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
374 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
|
375 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
376 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
377 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
378 AVExpr * ff_parse(const char *s, const char * const *const_name, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
379 double (**func1)(void *, double), const char **func1_name, |
8320 | 380 double (**func2)(void *, double, double), const char **func2_name, |
6324 | 381 const char **error){ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
382 Parser p; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
383 AVExpr *e = NULL; |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
384 char *w = av_malloc(strlen(s) + 1); |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
385 char *wp = w; |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
386 |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
387 if (!w) |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
388 goto end; |
4095 | 389 |
390 while (*s) | |
391 if (!isspace(*s++)) *wp++ = s[-1]; | |
392 *wp++ = 0; | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
393 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
394 p.stack_index=100; |
4095 | 395 p.s= w; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
396 p.const_name = const_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
397 p.func1 = func1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
398 p.func1_name = func1_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
399 p.func2 = func2; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
400 p.func2_name = func2_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
401 p.error= error; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
402 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
403 e = parse_expr(&p); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
404 if (!verify_expr(e)) { |
11597 | 405 ff_free_expr(e); |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
406 e = NULL; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
407 } |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
408 end: |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
409 av_free(w); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
410 return e; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
411 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
412 |
11604 | 413 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
|
414 Parser p; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
415 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
416 p.const_value= const_value; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
417 p.opaque = opaque; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
418 return eval_expr(&p, e); |
612 | 419 } |
420 | |
11605 | 421 double ff_parse_and_eval_expr(const char *s, const double *const_value, const char * const *const_name, |
1057 | 422 double (**func1)(void *, double), const char **func1_name, |
8320 | 423 double (**func2)(void *, double, double), const char **func2_name, |
6324 | 424 void *opaque, const char **error){ |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
425 AVExpr * e = ff_parse(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
|
426 double d; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
427 if (!e) return NAN; |
11604 | 428 d = ff_eval_expr(e, const_value, opaque); |
11597 | 429 ff_free_expr(e); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
430 return d; |
612 | 431 } |
2433 | 432 |
433 #ifdef TEST | |
2967 | 434 #undef printf |
2433 | 435 static double const_values[]={ |
436 M_PI, | |
437 M_E, | |
438 0 | |
439 }; | |
440 static const char *const_names[]={ | |
441 "PI", | |
442 "E", | |
443 0 | |
444 }; | |
6167 | 445 int main(void){ |
2436 | 446 int i; |
11605 | 447 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)); |
448 printf("%f == 0.931322575\n", ff_parse_and_eval_expr("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL)); | |
2967 | 449 |
2436 | 450 for(i=0; i<1050; i++){ |
451 START_TIMER | |
11605 | 452 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); |
453 STOP_TIMER("ff_parse_and_eval_expr") | |
2436 | 454 } |
6167 | 455 return 0; |
2433 | 456 } |
457 #endif |