annotate eval.c @ 3806:e1986d9ddc2d libavcodec

Fix compilation with --disable-encoders. patch by Alexander Strange, astrange at ithinksw dot com
author diego
date Sun, 01 Oct 2006 18:19:49 +0000
parents 3f7aa9fa5c98
children 3a151ccc6ed7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
1 /*
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
2 * simple arithmetic expression evaluator
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
3 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
5 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
10 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
14 * Lesser General Public License for more details.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
15 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
19 *
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 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
23 * @file eval.c
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
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
29 #include "avcodec.h"
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
30 #include "mpegvideo.h"
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
31
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
32 #include <stdio.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
33 #include <stdlib.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
34 #include <string.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
35 #include <math.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
36
614
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
37 #ifndef NAN
3753
82ceab4ada49 Define NAN -if not already defined- as 0.0/0.0
takis
parents: 3731
diff changeset
38 #define NAN 0.0/0.0
614
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
39 #endif
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
40
627
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
41 #ifndef M_PI
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
42 #define M_PI 3.14159265358979323846
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
43 #endif
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
44
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
45 typedef struct Parser{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
46 int stack_index;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
47 char *s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
48 double *const_value;
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
49 const char **const_name; // NULL terminated
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
50 double (**func1)(void *, double a); // NULL terminated
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
51 const char **func1_name; // NULL terminated
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
52 double (**func2)(void *, double a, double b); // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
53 char **func2_name; // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
54 void *opaque;
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
55 char **error;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
56 } Parser;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
57
3778
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
58 static double evalExpression(Parser *p);
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
59
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
60 static int8_t si_prefixes['z' - 'E' + 1]={
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
61 ['y'-'E']= -24,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
62 ['z'-'E']= -21,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
63 ['a'-'E']= -18,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
64 ['f'-'E']= -15,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
65 ['p'-'E']= -12,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
66 ['n'-'E']= - 9,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
67 ['u'-'E']= - 6,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
68 ['m'-'E']= - 3,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
69 ['c'-'E']= - 2,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
70 ['d'-'E']= - 1,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
71 ['h'-'E']= 2,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
72 ['k'-'E']= 3,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
73 ['K'-'E']= 3,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
74 ['M'-'E']= 6,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
75 ['G'-'E']= 9,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
76 ['T'-'E']= 12,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
77 ['P'-'E']= 15,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
78 ['E'-'E']= 18,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
79 ['Z'-'E']= 21,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
80 ['Y'-'E']= 24,
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
81 };
3756
9813c594dac5 Missing extern declaration for av_strtod.
takis
parents: 3755
diff changeset
82
3778
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
83 /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
84 * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
85 * function assumes that the unit of numbers is bits not bytes.
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
86 */
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
87 static double av_strtod(const char *name, char **tail) {
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
88 double d;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
89 int p = 0;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
90 char *next;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
91 d = strtod(name, &next);
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
92 /* if parsing succeeded, check for and interpret postfixes */
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
93 if (next!=name) {
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
94
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
95 if(*next >= 'E' && *next <= 'z'){
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
96 int e= si_prefixes[*next - 'E'];
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
97 if(e){
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
98 if(next[1] == 'i'){
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
99 d*= pow( 2, e/0.3);
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
100 next+=2;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
101 }else{
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
102 d*= pow(10, e);
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
103 next++;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
104 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
105 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
106 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
107
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
108 if(*next=='B') {
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
109 d*=8;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
110 *next++;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
111 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
112 }
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
113 /* 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
114 character */
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
115 if (tail)
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
116 *tail = next;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
117 return d;
67a63fa775a7 Make AVOption parsign code use ff_eval2()
takis
parents: 3774
diff changeset
118 }
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
119
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
120 static int strmatch(const char *s, const char *prefix){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
121 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
122 for(i=0; prefix[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
123 if(prefix[i] != s[i]) return 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
124 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
125 return 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
126 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
127
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
128 static double evalPrimary(Parser *p){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
129 double d, d2=NAN;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
130 char *next= p->s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
131 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
132
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
133 /* number */
3731
8b8773577dd9 Add support for SI (k, M, ...) and IEC/IEEE (Ki, Mi, ...) units.
takis
parents: 3730
diff changeset
134 d= av_strtod(p->s, &next);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
135 if(next != p->s){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
136 p->s= next;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
137 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
138 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
139
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
140 /* named constants */
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
141 for(i=0; p->const_name && p->const_name[i]; i++){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
142 if(strmatch(p->s, p->const_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
143 p->s+= strlen(p->const_name[i]);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
144 return p->const_value[i];
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
145 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
146 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
147
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
148 p->s= strchr(p->s, '(');
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
149 if(p->s==NULL){
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
150 *p->error = "missing (";
3754
6f219b6839ba segfault fix
michael
parents: 3753
diff changeset
151 p->s= next;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
152 return NAN;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
153 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
154 p->s++; // "("
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
155 d= evalExpression(p);
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
156 if(p->s[0]== ','){
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
157 p->s++; // ","
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
158 d2= evalExpression(p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
159 }
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
160 if(p->s[0] != ')'){
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
161 *p->error = "missing )";
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
162 return NAN;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
163 }
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
164 p->s++; // ")"
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
165
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
166 if( strmatch(next, "sinh" ) ) d= sinh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
167 else if( strmatch(next, "cosh" ) ) d= cosh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
168 else if( strmatch(next, "tanh" ) ) d= tanh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
169 else if( strmatch(next, "sin" ) ) d= sin(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
170 else if( strmatch(next, "cos" ) ) d= cos(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
171 else if( strmatch(next, "tan" ) ) d= tan(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
172 else if( strmatch(next, "exp" ) ) d= exp(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
173 else if( strmatch(next, "log" ) ) d= log(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
174 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
175 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
176 else if( strmatch(next, "abs" ) ) d= fabs(d);
3755
882cf925ff7a cosmetic
michael
parents: 3754
diff changeset
177 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
882cf925ff7a cosmetic
michael
parents: 3754
diff changeset
178 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
882cf925ff7a cosmetic
michael
parents: 3754
diff changeset
179 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
882cf925ff7a cosmetic
michael
parents: 3754
diff changeset
180 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
882cf925ff7a cosmetic
michael
parents: 3754
diff changeset
181 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
882cf925ff7a cosmetic
michael
parents: 3754
diff changeset
182 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
183 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
184 else if( strmatch(next, "(" ) ) d= d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
185 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
186 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
187 else{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
188 for(i=0; p->func1_name && p->func1_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
189 if(strmatch(next, p->func1_name[i])){
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
190 return p->func1[i](p->opaque, d);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
191 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
192 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
193
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
194 for(i=0; p->func2_name && p->func2_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
195 if(strmatch(next, p->func2_name[i])){
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
196 return p->func2[i](p->opaque, d, d2);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
197 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
198 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
199
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
200 *p->error = "unknown function";
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
201 return NAN;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
202 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
203
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
204 return d;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
205 }
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
206
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
207 static double evalPow(Parser *p){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
208 int sign= (*p->s == '+') - (*p->s == '-');
86d14aebd527 simplify
michael
parents: 2434
diff changeset
209 p->s += sign&1;
86d14aebd527 simplify
michael
parents: 2434
diff changeset
210 return (sign|1) * evalPrimary(p);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
211 }
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
212
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
213 static double evalFactor(Parser *p){
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
214 double ret= evalPow(p);
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
215 while(p->s[0]=='^'){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
216 p->s++;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
217 ret= pow(ret, evalPow(p));
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
218 }
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
219 return ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
220 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
221
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
222 static double evalTerm(Parser *p){
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
223 double ret= evalFactor(p);
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
224 while(p->s[0]=='*' || p->s[0]=='/'){
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
225 if(*p->s++ == '*') ret*= evalFactor(p);
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
226 else ret/= evalFactor(p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
227 }
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
228 return ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
229 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
230
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
231 static double evalExpression(Parser *p){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
232 double ret= 0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
233
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
234 if(p->stack_index <= 0) //protect against stack overflows
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
235 return NAN;
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
236 p->stack_index--;
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
237
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
238 do{
86d14aebd527 simplify
michael
parents: 2434
diff changeset
239 ret += evalTerm(p);
86d14aebd527 simplify
michael
parents: 2434
diff changeset
240 }while(*p->s == '+' || *p->s == '-');
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
241
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
242 p->stack_index++;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
243
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
244 return ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
245 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
246
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
247 double ff_eval2(char *s, double *const_value, const char **const_name,
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
248 double (**func1)(void *, double), const char **func1_name,
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
249 double (**func2)(void *, double, double), char **func2_name,
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
250 void *opaque, char **error){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
251 Parser p;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
252
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
253 p.stack_index=100;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
254 p.s= s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
255 p.const_value= const_value;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
256 p.const_name = const_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
257 p.func1 = func1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
258 p.func1_name = func1_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
259 p.func2 = func2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
260 p.func2_name = func2_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
261 p.opaque = opaque;
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
262 p.error= error;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
263
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
264 return evalExpression(&p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
265 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
266
3779
3f7aa9fa5c98 Break compatibility only when first part of version number changes, in this
takis
parents: 3778
diff changeset
267 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
3770
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
268 attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
269 double (**func1)(void *, double), const char **func1_name,
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
270 double (**func2)(void *, double, double), char **func2_name,
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
271 void *opaque){
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
272 char *error=NULL;
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
273 double ret;
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
274 ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
275 if (error)
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
276 av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
277 return ret;
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
278 }
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
279 #endif
ea345e1e440f Introduce ff_eval2 which is equivalent to ff_eval but does not log anything.
takis
parents: 3756
diff changeset
280
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
281 #ifdef TEST
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
282 #undef printf
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
283 static double const_values[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
284 M_PI,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
285 M_E,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
286 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
287 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
288 static const char *const_names[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
289 "PI",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
290 "E",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
291 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
292 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
293 main(){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
294 int i;
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
295 printf("%f == 12.7\n", ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
3730
aae4aed137ea K prefix
michael
parents: 3729
diff changeset
296 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
297
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
298 for(i=0; i<1050; i++){
86d14aebd527 simplify
michael
parents: 2434
diff changeset
299 START_TIMER
86d14aebd527 simplify
michael
parents: 2434
diff changeset
300 ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL);
86d14aebd527 simplify
michael
parents: 2434
diff changeset
301 STOP_TIMER("ff_eval")
86d14aebd527 simplify
michael
parents: 2434
diff changeset
302 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
303 }
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
304 #endif