Mercurial > libavcodec.hg
annotate eval.c @ 11801:026edf66e3a9 libavcodec
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
containing an error code.
Allow these functions to convey the reason of the failure to the
calling function, failure which is not always due to a parsing error
but it may depend for example on a memory problem.
Also fix several potential memleaks.
author | stefano |
---|---|
date | Tue, 01 Jun 2010 08:07:07 +0000 |
parents | c6368258b694 |
children | 5880d90f2b99 |
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{ |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
33 const AVClass *class; |
612 | 34 int stack_index; |
35 char *s; | |
8320 | 36 const double *const_value; |
37 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
|
38 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
|
39 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
|
40 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
|
41 const char * const *func2_name; // NULL terminated |
612 | 42 void *opaque; |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
43 int log_offset; |
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
44 void *log_ctx; |
4089 | 45 #define VARS 10 |
46 double var[VARS]; | |
612 | 47 } Parser; |
48 | |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
49 static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) }; |
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
50 |
7129 | 51 static const int8_t si_prefixes['z' - 'E' + 1]={ |
3778 | 52 ['y'-'E']= -24, |
53 ['z'-'E']= -21, | |
54 ['a'-'E']= -18, | |
55 ['f'-'E']= -15, | |
56 ['p'-'E']= -12, | |
57 ['n'-'E']= - 9, | |
58 ['u'-'E']= - 6, | |
59 ['m'-'E']= - 3, | |
60 ['c'-'E']= - 2, | |
61 ['d'-'E']= - 1, | |
62 ['h'-'E']= 2, | |
63 ['k'-'E']= 3, | |
64 ['K'-'E']= 3, | |
65 ['M'-'E']= 6, | |
66 ['G'-'E']= 9, | |
67 ['T'-'E']= 12, | |
68 ['P'-'E']= 15, | |
69 ['E'-'E']= 18, | |
70 ['Z'-'E']= 21, | |
71 ['Y'-'E']= 24, | |
72 }; | |
3756 | 73 |
9880 | 74 double av_strtod(const char *numstr, char **tail) { |
3778 | 75 double d; |
76 char *next; | |
9879
d54ba41c7e48
Cosmetics: rename 'name' av_strtod() param to 'numstr'. The new name
stefano
parents:
8718
diff
changeset
|
77 d = strtod(numstr, &next); |
3778 | 78 /* 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
|
79 if (next!=numstr) { |
3778 | 80 |
81 if(*next >= 'E' && *next <= 'z'){ | |
82 int e= si_prefixes[*next - 'E']; | |
83 if(e){ | |
84 if(next[1] == 'i'){ | |
85 d*= pow( 2, e/0.3); | |
86 next+=2; | |
87 }else{ | |
88 d*= pow(10, e); | |
89 next++; | |
90 } | |
91 } | |
92 } | |
93 | |
94 if(*next=='B') { | |
95 d*=8; | |
4355 | 96 next++; |
3778 | 97 } |
98 } | |
99 /* if requested, fill in tail with the position after the last parsed | |
100 character */ | |
101 if (tail) | |
102 *tail = next; | |
103 return d; | |
104 } | |
612 | 105 |
1057 | 106 static int strmatch(const char *s, const char *prefix){ |
612 | 107 int i; |
108 for(i=0; prefix[i]; i++){ | |
109 if(prefix[i] != s[i]) return 0; | |
110 } | |
111 return 1; | |
112 } | |
113 | |
11601
29fda2500178
Avoid the use of the symbol ff_expr_s for referencing AVExpr.
stefano
parents:
11597
diff
changeset
|
114 struct AVExpr { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
115 enum { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
116 e_value, e_const, e_func0, e_func1, e_func2, |
4089 | 117 e_squish, e_gauss, e_ld, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
118 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
|
119 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
|
120 e_last, e_st, e_while, |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
121 } type; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
122 double value; // is sign in other types |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
123 union { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
124 int const_index; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
125 double (*func0)(double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
126 double (*func1)(void *, double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
127 double (*func2)(void *, double, double); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
128 } a; |
11601
29fda2500178
Avoid the use of the symbol ff_expr_s for referencing AVExpr.
stefano
parents:
11597
diff
changeset
|
129 struct AVExpr *param[2]; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
130 }; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
131 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
132 static double eval_expr(Parser * p, AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
133 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
134 case e_value: return e->value; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); } |
4594 | 141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 return d; |
8e35dfc4ae15
add support for while() loops again ugly syntax while(condition, statements) but very simple implementation
michael
parents:
4089
diff
changeset
|
147 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
148 default: { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
149 double d = eval_expr(p, e->param[0]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
150 double d2 = eval_expr(p, e->param[1]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
151 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 case e_pow: return e->value * pow(d, d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
159 case e_mul: return e->value * (d * d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
160 case e_div: return e->value * (d / d2); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
161 case e_add: return e->value * (d + d2); |
4093 | 162 case e_last:return e->value * d2; |
4594 | 163 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
|
164 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
165 } |
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 return NAN; |
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 |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
170 static int parse_expr(AVExpr **e, Parser *p); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
171 |
11597 | 172 void ff_free_expr(AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
173 if (!e) return; |
11597 | 174 ff_free_expr(e->param[0]); |
175 ff_free_expr(e->param[1]); | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
176 av_freep(&e); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
177 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
178 |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
179 static int parse_primary(AVExpr **e, Parser *p) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
180 { |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
181 AVExpr * d = av_mallocz(sizeof(AVExpr)); |
612 | 182 char *next= p->s; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
183 int ret, i; |
612 | 184 |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
185 if (!d) |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
186 return AVERROR(ENOMEM); |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
187 |
612 | 188 /* number */ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
189 d->value = av_strtod(p->s, &next); |
612 | 190 if(next != p->s){ |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
191 d->type = e_value; |
612 | 192 p->s= next; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
193 *e = d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
194 return 0; |
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; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
204 *e = d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
205 return 0; |
612 | 206 } |
207 } | |
2967 | 208 |
612 | 209 p->s= strchr(p->s, '('); |
210 if(p->s==NULL){ | |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
211 av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n"); |
3754 | 212 p->s= next; |
11597 | 213 ff_free_expr(d); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
214 return AVERROR(EINVAL); |
612 | 215 } |
216 p->s++; // "(" | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
217 if (*next == '(') { // special case do-nothing |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
218 av_freep(&d); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
219 if ((ret = parse_expr(&d, p)) < 0) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
220 return ret; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
221 if(p->s[0] != ')'){ |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
222 av_log(p, AV_LOG_ERROR, "missing )\n"); |
11597 | 223 ff_free_expr(d); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
224 return AVERROR(EINVAL); |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
225 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
226 p->s++; // ")" |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
227 *e = d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
228 return 0; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
229 } |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
230 if ((ret = parse_expr(&(d->param[0]), p)) < 0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
231 ff_free_expr(d); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
232 return ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
233 } |
1815 | 234 if(p->s[0]== ','){ |
235 p->s++; // "," | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
236 parse_expr(&d->param[1], p); |
612 | 237 } |
1815 | 238 if(p->s[0] != ')'){ |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
239 av_log(p, AV_LOG_ERROR, "missing )\n"); |
11597 | 240 ff_free_expr(d); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
241 return AVERROR(EINVAL); |
1815 | 242 } |
243 p->s++; // ")" | |
2967 | 244 |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
245 d->type = e_func0; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
246 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
247 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
248 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
249 else if( strmatch(next, "sin" ) ) d->a.func0 = sin; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
250 else if( strmatch(next, "cos" ) ) d->a.func0 = cos; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
251 else if( strmatch(next, "tan" ) ) d->a.func0 = tan; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
252 else if( strmatch(next, "atan" ) ) d->a.func0 = atan; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
253 else if( strmatch(next, "asin" ) ) d->a.func0 = asin; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
254 else if( strmatch(next, "acos" ) ) d->a.func0 = acos; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
255 else if( strmatch(next, "exp" ) ) d->a.func0 = exp; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
256 else if( strmatch(next, "log" ) ) d->a.func0 = log; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
257 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
258 else if( strmatch(next, "squish") ) d->type = e_squish; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
259 else if( strmatch(next, "gauss" ) ) d->type = e_gauss; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
260 else if( strmatch(next, "mod" ) ) d->type = e_mod; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
261 else if( strmatch(next, "max" ) ) d->type = e_max; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
262 else if( strmatch(next, "min" ) ) d->type = e_min; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
263 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
|
264 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
|
265 else if( strmatch(next, "gt" ) ) d->type = e_gt; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
266 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
|
267 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 | 268 else if( strmatch(next, "ld" ) ) d->type = e_ld; |
269 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
|
270 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
|
271 else { |
612 | 272 for(i=0; p->func1_name && p->func1_name[i]; i++){ |
273 if(strmatch(next, p->func1_name[i])){ | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
274 d->a.func1 = p->func1[i]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
275 d->type = e_func1; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
276 *e = d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
277 return 0; |
612 | 278 } |
279 } | |
280 | |
281 for(i=0; p->func2_name && p->func2_name[i]; i++){ | |
282 if(strmatch(next, p->func2_name[i])){ | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
283 d->a.func2 = p->func2[i]; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
284 d->type = e_func2; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
285 *e = d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
286 return 0; |
612 | 287 } |
288 } | |
289 | |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
290 av_log(p, AV_LOG_ERROR, "unknown function\n"); |
11597 | 291 ff_free_expr(d); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
292 return AVERROR(EINVAL); |
612 | 293 } |
2433 | 294 |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
295 *e = d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
296 return 0; |
2967 | 297 } |
2436 | 298 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
299 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
|
300 AVExpr * e = av_mallocz(sizeof(AVExpr)); |
10168
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
301 if (!e) |
6eded00bb689
eval: Check for return value of memory allocations.
ramiro
parents:
10067
diff
changeset
|
302 return NULL; |
4085 | 303 e->type =type ; |
304 e->value =value ; | |
305 e->param[0] =p0 ; | |
306 e->param[1] =p1 ; | |
307 return e; | |
308 } | |
309 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
310 static int parse_pow(AVExpr **e, Parser *p, int *sign) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
311 { |
4032 | 312 *sign= (*p->s == '+') - (*p->s == '-'); |
313 p->s += *sign&1; | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
314 return parse_primary(e, p); |
2434 | 315 } |
612 | 316 |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
317 static int parse_factor(AVExpr **e, Parser *p) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
318 { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
319 int sign, sign2, ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
320 AVExpr *e0, *e1, *e2; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
321 if ((ret = parse_pow(&e0, p, &sign)) < 0) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
322 return ret; |
2434 | 323 while(p->s[0]=='^'){ |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
324 e1 = e0; |
612 | 325 p->s++; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
326 if ((ret = parse_pow(&e2, p, &sign2)) < 0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
327 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
328 return ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
329 } |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
330 e0 = new_eval_expr(e_pow, 1, e1, e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
331 if (!e0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
332 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
333 ff_free_expr(e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
334 return AVERROR(ENOMEM); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
335 } |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
336 if (e0->param[1]) e0->param[1]->value *= (sign2|1); |
612 | 337 } |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
338 if (e0) e0->value *= (sign|1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
339 |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
340 *e = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
341 return 0; |
612 | 342 } |
343 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
344 static int parse_term(AVExpr **e, Parser *p) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
345 { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
346 int ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
347 AVExpr *e0, *e1, *e2; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
348 if ((ret = parse_factor(&e0, p)) < 0) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
349 return ret; |
2434 | 350 while(p->s[0]=='*' || p->s[0]=='/'){ |
4085 | 351 int c= *p->s++; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
352 e1 = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
353 if ((ret = parse_factor(&e2, p)) < 0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
354 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
355 return ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
356 } |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
357 e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
358 if (!e0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
359 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
360 ff_free_expr(e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
361 return AVERROR(ENOMEM); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
362 } |
612 | 363 } |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
364 *e = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
365 return 0; |
612 | 366 } |
367 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
368 static int parse_subexpr(AVExpr **e, Parser *p) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
369 { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
370 int ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
371 AVExpr *e0, *e1, *e2; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
372 if ((ret = parse_term(&e0, p)) < 0) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
373 return ret; |
4089 | 374 while(*p->s == '+' || *p->s == '-') { |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
375 e1 = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
376 if ((ret = parse_term(&e2, p)) < 0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
377 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
378 return ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
379 } |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
380 e0 = new_eval_expr(e_add, 1, e1, e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
381 if (!e0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
382 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
383 ff_free_expr(e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
384 return AVERROR(ENOMEM); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
385 } |
4089 | 386 }; |
387 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
388 *e = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
389 return 0; |
4089 | 390 } |
391 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
392 static int parse_expr(AVExpr **e, Parser *p) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
393 { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
394 int ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
395 AVExpr *e0, *e1, *e2; |
2434 | 396 if(p->stack_index <= 0) //protect against stack overflows |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
397 return AVERROR(EINVAL); |
2434 | 398 p->stack_index--; |
399 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
400 if ((ret = parse_subexpr(&e0, p)) < 0) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
401 return ret; |
4089 | 402 while(*p->s == ';') { |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
403 e1 = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
404 if ((ret = parse_subexpr(&e2, p)) < 0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
405 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
406 return ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
407 } |
4089 | 408 p->s++; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
409 e0 = new_eval_expr(e_last, 1, e1, e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
410 if (!e0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
411 ff_free_expr(e1); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
412 ff_free_expr(e2); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
413 return AVERROR(ENOMEM); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
414 } |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
415 }; |
612 | 416 |
2434 | 417 p->stack_index++; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
418 *e = e0; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
419 return 0; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
420 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
421 |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
422 static int verify_expr(AVExpr * e) { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
423 if (!e) return 0; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
424 switch (e->type) { |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
425 case e_value: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
426 case e_const: return 1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
427 case e_func0: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
428 case e_func1: |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
429 case e_squish: |
4089 | 430 case e_ld: |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
431 case e_gauss: return verify_expr(e->param[0]); |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
432 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
|
433 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
434 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
435 |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
436 int ff_parse_expr(AVExpr **expr, const char *s, |
11739
ceffa0ca7596
Change the order of parameters for ff_eval_expr() and
stefano
parents:
11644
diff
changeset
|
437 const char * const *const_name, |
ceffa0ca7596
Change the order of parameters for ff_eval_expr() and
stefano
parents:
11644
diff
changeset
|
438 const char * const *func1_name, double (* const *func1)(void *, double), |
ceffa0ca7596
Change the order of parameters for ff_eval_expr() and
stefano
parents:
11644
diff
changeset
|
439 const char * const *func2_name, double (* const *func2)(void *, double, double), |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
440 int log_offset, void *log_ctx) |
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
441 { |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
442 Parser p; |
11596
e22a96273dc4
Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents:
10168
diff
changeset
|
443 AVExpr *e = NULL; |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
444 char *w = av_malloc(strlen(s) + 1); |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
445 char *wp = w; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
446 int ret = 0; |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
447 |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
448 if (!w) |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
449 return AVERROR(ENOMEM); |
4095 | 450 |
451 while (*s) | |
452 if (!isspace(*s++)) *wp++ = s[-1]; | |
453 *wp++ = 0; | |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
454 |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
455 p.class = &class; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
456 p.stack_index=100; |
4095 | 457 p.s= w; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
458 p.const_name = const_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
459 p.func1 = func1; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
460 p.func1_name = func1_name; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
461 p.func2 = func2; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
462 p.func2_name = func2_name; |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
463 p.log_offset = log_offset; |
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
464 p.log_ctx = log_ctx; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
465 |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
466 if ((ret = parse_expr(&e, &p)) < 0) |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
467 goto end; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
468 if (!verify_expr(e)) { |
11597 | 469 ff_free_expr(e); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
470 ret = AVERROR(EINVAL); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
471 goto end; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
472 } |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
473 *expr = e; |
10067
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
474 end: |
685af2860d80
eval: replace variable-length array with av_malloc/free
mru
parents:
10040
diff
changeset
|
475 av_free(w); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
476 return ret; |
4081
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
477 } |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
478 |
11604 | 479 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
|
480 Parser p; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
481 |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
482 p.const_value= const_value; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
483 p.opaque = opaque; |
cedb63307f3d
new optimized eval method, by seperating parsing and runtime
ods15
parents:
4032
diff
changeset
|
484 return eval_expr(&p, e); |
612 | 485 } |
486 | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
487 int ff_parse_and_eval_expr(double *d, const char *s, |
11739
ceffa0ca7596
Change the order of parameters for ff_eval_expr() and
stefano
parents:
11644
diff
changeset
|
488 const char * const *const_name, const double *const_value, |
ceffa0ca7596
Change the order of parameters for ff_eval_expr() and
stefano
parents:
11644
diff
changeset
|
489 const char * const *func1_name, double (* const *func1)(void *, double), |
ceffa0ca7596
Change the order of parameters for ff_eval_expr() and
stefano
parents:
11644
diff
changeset
|
490 const char * const *func2_name, double (* const *func2)(void *, double, double), |
11749
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
491 void *opaque, int log_offset, void *log_ctx) |
c6368258b694
Change eval API to take parent log context and log level offset.
michael
parents:
11739
diff
changeset
|
492 { |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
493 AVExpr *e = NULL; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
494 int ret = ff_parse_expr(&e, s, const_name, func1_name, func1, func2_name, func2, log_offset, log_ctx); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
495 |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
496 if (ret < 0) { |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
497 *d = NAN; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
498 return ret; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
499 } |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
500 *d = ff_eval_expr(e, const_value, opaque); |
11597 | 501 ff_free_expr(e); |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
502 return isnan(*d) ? AVERROR(EINVAL) : 0; |
612 | 503 } |
2433 | 504 |
505 #ifdef TEST | |
2967 | 506 #undef printf |
2433 | 507 static double const_values[]={ |
508 M_PI, | |
509 M_E, | |
510 0 | |
511 }; | |
512 static const char *const_names[]={ | |
513 "PI", | |
514 "E", | |
515 0 | |
516 }; | |
6167 | 517 int main(void){ |
2436 | 518 int i; |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
519 double d; |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
520 ff_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
521 printf("%f == 12.7\n", d); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
522 ff_parse_and_eval_expr(&d, "80G/80Gi", const_names, const_values, NULL, NULL, NULL, NULL, NULL, NULL); |
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
523 printf("%f == 0.931322575\n", d); |
2967 | 524 |
2436 | 525 for(i=0; i<1050; i++){ |
526 START_TIMER | |
11801
026edf66e3a9
Make ff_parse_expr() and ff_parse_and_eval_expr() return an int
stefano
parents:
11749
diff
changeset
|
527 ff_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL); |
11605 | 528 STOP_TIMER("ff_parse_and_eval_expr") |
2436 | 529 } |
6167 | 530 return 0; |
2433 | 531 } |
532 #endif |