annotate eval.c @ 11779:9371254837b5 libavcodec

Add support for the newly added Nut codec tags (added in Nut r669): Y1[00][16], [16][00]1Y, Y3[11][16], [16][11]3Y, Y3[10][16], [16][10]3Y, Y3[00][16], [16][00]3Y, Y4[11][ 8], Y2[00][ 8].
author stefano
date Wed, 26 May 2010 22:26:19 +0000
parents c6368258b694
children 026edf66e3a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
1 /*
4101
a15abf56debf update copyright year, it looks odd otherwise :)
michael
parents: 4099
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
15 * Lesser General Public License for more details.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
16 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
20 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
21
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
22 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11616
diff changeset
23 * @file
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
24 * simple arithmetic expression evaluator.
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
25 *
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
27 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
28
11613
2c41284050cf Remove unnecessary header inclusion directives.
stefano
parents: 11605
diff changeset
29 #include "libavutil/avutil.h"
10039
e557e75172b0 eval: include our headers after system headers
mru
parents: 9880
diff changeset
30 #include "eval.h"
e557e75172b0 eval: include our headers after system headers
mru
parents: 9880
diff changeset
31
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
34 int stack_index;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
35 char *s;
8320
0de488aa4fb0 Add a few const qualifiers in appropriate places.
diego
parents: 8108
diff changeset
36 const double *const_value;
0de488aa4fb0 Add a few const qualifiers in appropriate places.
diego
parents: 8108
diff changeset
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
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
45 #define VARS 10
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
46 double var[VARS];
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
47 } Parser;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
322023e630a6 mark read-only data as const
stefang
parents: 6840
diff changeset
51 static const int8_t si_prefixes['z' - 'E' + 1]={
3778
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
52 ['y'-'E']= -24,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
53 ['z'-'E']= -21,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
54 ['a'-'E']= -18,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
55 ['f'-'E']= -15,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
56 ['p'-'E']= -12,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
57 ['n'-'E']= - 9,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
58 ['u'-'E']= - 6,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
59 ['m'-'E']= - 3,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
60 ['c'-'E']= - 2,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
61 ['d'-'E']= - 1,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
62 ['h'-'E']= 2,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
63 ['k'-'E']= 3,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
64 ['K'-'E']= 3,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
65 ['M'-'E']= 6,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
66 ['G'-'E']= 9,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
67 ['T'-'E']= 12,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
68 ['P'-'E']= 15,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
69 ['E'-'E']= 18,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
70 ['Z'-'E']= 21,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
71 ['Y'-'E']= 24,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
72 };
3756
9813c594dac5 Missing extern declaration for av_strtod.
takis
parents: 3755
diff changeset
73
9880
e934c5f8f4a9 Export av_strtod() to eval.h.
stefano
parents: 9879
diff changeset
74 double av_strtod(const char *numstr, char **tail) {
3778
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
75 double d;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
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
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
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
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
80
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
81 if(*next >= 'E' && *next <= 'z'){
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
82 int e= si_prefixes[*next - 'E'];
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
83 if(e){
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
84 if(next[1] == 'i'){
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
85 d*= pow( 2, e/0.3);
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
86 next+=2;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
87 }else{
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
88 d*= pow(10, e);
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
89 next++;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
90 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
91 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
92 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
93
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
94 if(*next=='B') {
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
95 d*=8;
4355
085f24822713 fix a gcc warning, avoid an unnecessary operation
lu_zero
parents: 4101
diff changeset
96 next++;
3778
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
97 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
98 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
99 /* if requested, fill in tail with the position after the last parsed
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
100 character */
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
101 if (tail)
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
102 *tail = next;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
103 return d;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
104 }
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
105
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
106 static int strmatch(const char *s, const char *prefix){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
107 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
108 for(i=0; prefix[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
109 if(prefix[i] != s[i]) return 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
110 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
111 return 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
112 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
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
a96d905dcbaa Add av_ prefix to clip functions
reimar
parents: 4355
diff changeset
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
ca9b4b42ebf9 add missing 'e->value * '
ods15
parents: 4092
diff changeset
162 case e_last:return e->value * d2;
4594
a96d905dcbaa Add av_ prefix to clip functions
reimar
parents: 4355
diff changeset
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
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
170 static AVExpr * parse_expr(Parser *p);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
171
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
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
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
174 ff_free_expr(e->param[0]);
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
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
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
179 static AVExpr * parse_primary(Parser *p) {
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
180 AVExpr * d = av_mallocz(sizeof(AVExpr));
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
181 char *next= p->s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
182 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
183
10168
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
184 if (!d)
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
185 return NULL;
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
186
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
187 /* number */
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
188 d->value = av_strtod(p->s, &next);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
189 if(next != p->s){
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
190 d->type = e_value;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
191 p->s= next;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
192 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
193 }
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
194 d->value = 1;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
195
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
196 /* named constants */
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
197 for(i=0; p->const_name && p->const_name[i]; i++){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
198 if(strmatch(p->s, p->const_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
199 p->s+= strlen(p->const_name[i]);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
200 d->type = e_const;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
201 d->a.const_index = i;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
202 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
203 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
204 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
205
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
206 p->s= strchr(p->s, '(');
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
207 if(p->s==NULL){
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
208 av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n");
3754
6f219b6839ba segfault fix
michael
parents: 3753
diff changeset
209 p->s= next;
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
210 ff_free_expr(d);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
211 return NULL;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
212 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
213 p->s++; // "("
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
214 if (*next == '(') { // special case do-nothing
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
215 av_freep(&d);
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
216 d = parse_expr(p);
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
217 if(p->s[0] != ')'){
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
218 av_log(p, AV_LOG_ERROR, "missing )\n");
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
219 ff_free_expr(d);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
220 return NULL;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
221 }
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
222 p->s++; // ")"
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
223 return d;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
224 }
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
225 d->param[0] = parse_expr(p);
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
226 if(p->s[0]== ','){
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
227 p->s++; // ","
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
228 d->param[1] = parse_expr(p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
229 }
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
230 if(p->s[0] != ')'){
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
231 av_log(p, AV_LOG_ERROR, "missing )\n");
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
232 ff_free_expr(d);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
233 return NULL;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
234 }
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
235 p->s++; // ")"
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
236
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
237 d->type = e_func0;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
238 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
239 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
240 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
241 else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
242 else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
243 else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
244 else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
245 else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
246 else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
247 else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
248 else if( strmatch(next, "log" ) ) d->a.func0 = log;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
249 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
250 else if( strmatch(next, "squish") ) d->type = e_squish;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
251 else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
252 else if( strmatch(next, "mod" ) ) d->type = e_mod;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
253 else if( strmatch(next, "max" ) ) d->type = e_max;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
254 else if( strmatch(next, "min" ) ) d->type = e_min;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
255 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
256 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
257 else if( strmatch(next, "gt" ) ) d->type = e_gt;
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
258 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
259 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
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
260 else if( strmatch(next, "ld" ) ) d->type = e_ld;
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
261 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
262 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
263 else {
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
264 for(i=0; p->func1_name && p->func1_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
265 if(strmatch(next, p->func1_name[i])){
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
266 d->a.func1 = p->func1[i];
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
267 d->type = e_func1;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
268 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
269 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
270 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
271
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
272 for(i=0; p->func2_name && p->func2_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
273 if(strmatch(next, p->func2_name[i])){
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
274 d->a.func2 = p->func2[i];
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
275 d->type = e_func2;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
276 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
277 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
278 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
279
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
280 av_log(p, AV_LOG_ERROR, "unknown function\n");
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
281 ff_free_expr(d);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
282 return NULL;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
283 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
284
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
285 return d;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
286 }
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
287
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
288 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
289 AVExpr * e = av_mallocz(sizeof(AVExpr));
10168
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
290 if (!e)
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
291 return NULL;
4085
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
292 e->type =type ;
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
293 e->value =value ;
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
294 e->param[0] =p0 ;
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
295 e->param[1] =p1 ;
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
296 return e;
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
297 }
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
298
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
299 static AVExpr * parse_pow(Parser *p, int *sign){
4032
0f2bb0baf6f0 fix -a^b which was interpreted as (-a)^b
michael
parents: 4031
diff changeset
300 *sign= (*p->s == '+') - (*p->s == '-');
0f2bb0baf6f0 fix -a^b which was interpreted as (-a)^b
michael
parents: 4031
diff changeset
301 p->s += *sign&1;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
302 return parse_primary(p);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
303 }
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
304
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
305 static AVExpr * parse_factor(Parser *p){
4032
0f2bb0baf6f0 fix -a^b which was interpreted as (-a)^b
michael
parents: 4031
diff changeset
306 int sign, sign2;
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
307 AVExpr * e = parse_pow(p, &sign);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
308 while(p->s[0]=='^'){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
309 p->s++;
4086
9076b11ea35f minor simplification
michael
parents: 4085
diff changeset
310 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
311 if (!e)
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
312 return NULL;
4086
9076b11ea35f minor simplification
michael
parents: 4085
diff changeset
313 if (e->param[1]) e->param[1]->value *= (sign2|1);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
314 }
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
315 if (e) e->value *= (sign|1);
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
316 return e;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
317 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
318
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
319 static AVExpr * parse_term(Parser *p){
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
320 AVExpr * e = parse_factor(p);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
321 while(p->s[0]=='*' || p->s[0]=='/'){
4085
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
322 int c= *p->s++;
16b88d6b3546 factorize AVEvalExpr alloc and init
michael
parents: 4081
diff changeset
323 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
324 if (!e)
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
325 return NULL;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
326 }
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
327 return e;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
328 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
329
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
330 static AVExpr * parse_subexpr(Parser *p) {
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
331 AVExpr * e = parse_term(p);
4089
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
332 while(*p->s == '+' || *p->s == '-') {
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
333 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
334 if (!e)
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
335 return NULL;
4089
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
336 };
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
337
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
338 return e;
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
339 }
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
340
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
341 static AVExpr * parse_expr(Parser *p) {
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
342 AVExpr * e;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
343
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
344 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
345 return NULL;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
346 p->stack_index--;
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
347
4089
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
348 e = parse_subexpr(p);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
349
4089
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
350 while(*p->s == ';') {
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
351 p->s++;
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
352 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
353 if (!e)
6eded00bb689 eval: Check for return value of memory allocations.
ramiro
parents: 10067
diff changeset
354 return NULL;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
355 };
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
356
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
357 p->stack_index++;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
358
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
359 return e;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
360 }
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
361
11596
e22a96273dc4 Rename AVEvalExpr to AVExpr, as suggested by Michael.
stefano
parents: 10168
diff changeset
362 static int verify_expr(AVExpr * e) {
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
363 if (!e) return 0;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
364 switch (e->type) {
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
365 case e_value:
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
366 case e_const: return 1;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
367 case e_func0:
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
368 case e_func1:
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
369 case e_squish:
4089
4f5f752f732c support seperating expressons by ;
michael
parents: 4087
diff changeset
370 case e_ld:
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
371 case e_gauss: return verify_expr(e->param[0]);
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
372 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
373 }
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
374 }
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
375
11739
ceffa0ca7596 Change the order of parameters for ff_eval_expr() and
stefano
parents: 11644
diff changeset
376 AVExpr *ff_parse_expr(const char *s,
ceffa0ca7596 Change the order of parameters for ff_eval_expr() and
stefano
parents: 11644
diff changeset
377 const char * const *const_name,
ceffa0ca7596 Change the order of parameters for ff_eval_expr() and
stefano
parents: 11644
diff changeset
378 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
379 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
380 int log_offset, void *log_ctx)
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
381 {
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
fda3ec8e96e1 ignore whitespace in ff_eval
ods15
parents: 4093
diff changeset
389
fda3ec8e96e1 ignore whitespace in ff_eval
ods15
parents: 4093
diff changeset
390 while (*s)
fda3ec8e96e1 ignore whitespace in ff_eval
ods15
parents: 4093
diff changeset
391 if (!isspace(*s++)) *wp++ = s[-1];
fda3ec8e96e1 ignore whitespace in ff_eval
ods15
parents: 4093
diff changeset
392 *wp++ = 0;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
393
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
394 p.class = &class;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
395 p.stack_index=100;
4095
fda3ec8e96e1 ignore whitespace in ff_eval
ods15
parents: 4093
diff changeset
396 p.s= w;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
397 p.const_name = const_name;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
398 p.func1 = func1;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
399 p.func1_name = func1_name;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
400 p.func2 = func2;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
401 p.func2_name = func2_name;
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
402 p.log_offset = log_offset;
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
403 p.log_ctx = log_ctx;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
404
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
405 e = parse_expr(&p);
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
406 if (!verify_expr(e)) {
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
407 ff_free_expr(e);
10067
685af2860d80 eval: replace variable-length array with av_malloc/free
mru
parents: 10040
diff changeset
408 e = NULL;
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
409 }
10067
685af2860d80 eval: replace variable-length array with av_malloc/free
mru
parents: 10040
diff changeset
410 end:
685af2860d80 eval: replace variable-length array with av_malloc/free
mru
parents: 10040
diff changeset
411 av_free(w);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
412 return e;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
413 }
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
414
11604
6bab1bfac3bb Rename ff_parse_eval() to ff_eval_expr().
stefano
parents: 11602
diff changeset
415 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
416 Parser p;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
417
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
418 p.const_value= const_value;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
419 p.opaque = opaque;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
420 return eval_expr(&p, e);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
421 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
422
11739
ceffa0ca7596 Change the order of parameters for ff_eval_expr() and
stefano
parents: 11644
diff changeset
423 double ff_parse_and_eval_expr(const char *s,
ceffa0ca7596 Change the order of parameters for ff_eval_expr() and
stefano
parents: 11644
diff changeset
424 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
425 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
426 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
427 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
428 {
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
429 AVExpr *e = ff_parse_expr(s, const_name, func1_name, func1, func2_name, func2, log_offset, log_ctx);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
430 double d;
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
431 if (!e) return NAN;
11604
6bab1bfac3bb Rename ff_parse_eval() to ff_eval_expr().
stefano
parents: 11602
diff changeset
432 d = ff_eval_expr(e, const_value, opaque);
11597
fce0ed54244c Rename ff_eval_free() to ff_free_expr().
stefano
parents: 11596
diff changeset
433 ff_free_expr(e);
4081
cedb63307f3d new optimized eval method, by seperating parsing and runtime
ods15
parents: 4032
diff changeset
434 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
435 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
436
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
437 #ifdef TEST
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
438 #undef printf
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
439 static double const_values[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
440 M_PI,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
441 M_E,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
442 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
443 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
444 static const char *const_names[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
445 "PI",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
446 "E",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
447 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
448 };
6167
0ae80d7e989a Fix warnings in test code:
diego
parents: 5934
diff changeset
449 int main(void){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
450 int i;
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
451 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_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL));
11739
ceffa0ca7596 Change the order of parameters for ff_eval_expr() and
stefano
parents: 11644
diff changeset
452 printf("%f == 0.931322575\n", ff_parse_and_eval_expr("80G/80Gi", const_names, const_values, NULL, NULL, NULL, NULL, NULL, NULL));
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
453
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
454 for(i=0; i<1050; i++){
86d14aebd527 simplify
michael
parents: 2434
diff changeset
455 START_TIMER
11749
c6368258b694 Change eval API to take parent log context and log level offset.
michael
parents: 11739
diff changeset
456 ff_parse_and_eval_expr("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
414a7cdaa54d Rename ff_eval2() to ff_parse_and_eval_expr().
stefano
parents: 11604
diff changeset
457 STOP_TIMER("ff_parse_and_eval_expr")
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
458 }
6167
0ae80d7e989a Fix warnings in test code:
diego
parents: 5934
diff changeset
459 return 0;
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
460 }
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
461 #endif