Originally committed as revision 6803 to svn://svn.ffmpeg.org/ffmpeg/trunk
| ... | ... |
@@ -30,6 +30,7 @@ |
| 30 | 30 |
|
| 31 | 31 |
#include "avcodec.h" |
| 32 | 32 |
#include "mpegvideo.h" |
| 33 |
+#include "eval.h" |
|
| 33 | 34 |
|
| 34 | 35 |
#include <stdio.h> |
| 35 | 36 |
#include <stdlib.h> |
| ... | ... |
@@ -57,8 +58,6 @@ typedef struct Parser{
|
| 57 | 57 |
char **error; |
| 58 | 58 |
} Parser; |
| 59 | 59 |
|
| 60 |
-static double evalExpression(Parser *p); |
|
| 61 |
- |
|
| 62 | 60 |
static int8_t si_prefixes['z' - 'E' + 1]={
|
| 63 | 61 |
['y'-'E']= -24, |
| 64 | 62 |
['z'-'E']= -21, |
| ... | ... |
@@ -126,23 +125,82 @@ static int strmatch(const char *s, const char *prefix){
|
| 126 | 126 |
return 1; |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 |
-static double evalPrimary(Parser *p){
|
|
| 130 |
- double d, d2=NAN; |
|
| 129 |
+struct ff_expr_s {
|
|
| 130 |
+ enum {
|
|
| 131 |
+ e_value, e_const, e_func0, e_func1, e_func2, |
|
| 132 |
+ e_squish, e_gauss, |
|
| 133 |
+ e_mod, e_max, e_min, e_eq, e_gt, e_gte, |
|
| 134 |
+ e_pow, e_mul, e_div, e_add, |
|
| 135 |
+ } type; |
|
| 136 |
+ double value; // is sign in other types |
|
| 137 |
+ union {
|
|
| 138 |
+ int const_index; |
|
| 139 |
+ double (*func0)(double); |
|
| 140 |
+ double (*func1)(void *, double); |
|
| 141 |
+ double (*func2)(void *, double, double); |
|
| 142 |
+ } a; |
|
| 143 |
+ AVEvalExpr * param[2]; |
|
| 144 |
+}; |
|
| 145 |
+ |
|
| 146 |
+static double eval_expr(Parser * p, AVEvalExpr * e) {
|
|
| 147 |
+ switch (e->type) {
|
|
| 148 |
+ case e_value: return e->value; |
|
| 149 |
+ case e_const: return e->value * p->const_value[e->a.const_index]; |
|
| 150 |
+ case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0])); |
|
| 151 |
+ case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0])); |
|
| 152 |
+ case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1])); |
|
| 153 |
+ case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0]))); |
|
| 154 |
+ case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
|
|
| 155 |
+ default: {
|
|
| 156 |
+ double d = eval_expr(p, e->param[0]); |
|
| 157 |
+ double d2 = eval_expr(p, e->param[1]); |
|
| 158 |
+ switch (e->type) {
|
|
| 159 |
+ case e_mod: return e->value * (d - floor(d/d2)*d2); |
|
| 160 |
+ case e_max: return e->value * (d > d2 ? d : d2); |
|
| 161 |
+ case e_min: return e->value * (d < d2 ? d : d2); |
|
| 162 |
+ case e_eq: return e->value * (d == d2 ? 1.0 : 0.0); |
|
| 163 |
+ case e_gt: return e->value * (d > d2 ? 1.0 : 0.0); |
|
| 164 |
+ case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0); |
|
| 165 |
+ case e_pow: return e->value * pow(d, d2); |
|
| 166 |
+ case e_mul: return e->value * (d * d2); |
|
| 167 |
+ case e_div: return e->value * (d / d2); |
|
| 168 |
+ case e_add: return e->value * (d + d2); |
|
| 169 |
+ } |
|
| 170 |
+ } |
|
| 171 |
+ } |
|
| 172 |
+ return NAN; |
|
| 173 |
+} |
|
| 174 |
+ |
|
| 175 |
+static AVEvalExpr * parse_expr(Parser *p); |
|
| 176 |
+ |
|
| 177 |
+void ff_eval_free(AVEvalExpr * e) {
|
|
| 178 |
+ if (!e) return; |
|
| 179 |
+ ff_eval_free(e->param[0]); |
|
| 180 |
+ ff_eval_free(e->param[1]); |
|
| 181 |
+ av_freep(&e); |
|
| 182 |
+} |
|
| 183 |
+ |
|
| 184 |
+static AVEvalExpr * parse_primary(Parser *p) {
|
|
| 185 |
+ AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr)); |
|
| 131 | 186 |
char *next= p->s; |
| 132 | 187 |
int i; |
| 133 | 188 |
|
| 134 | 189 |
/* number */ |
| 135 |
- d= av_strtod(p->s, &next); |
|
| 190 |
+ d->value = av_strtod(p->s, &next); |
|
| 136 | 191 |
if(next != p->s){
|
| 192 |
+ d->type = e_value; |
|
| 137 | 193 |
p->s= next; |
| 138 | 194 |
return d; |
| 139 | 195 |
} |
| 196 |
+ d->value = 1; |
|
| 140 | 197 |
|
| 141 | 198 |
/* named constants */ |
| 142 | 199 |
for(i=0; p->const_name && p->const_name[i]; i++){
|
| 143 | 200 |
if(strmatch(p->s, p->const_name[i])){
|
| 144 | 201 |
p->s+= strlen(p->const_name[i]); |
| 145 |
- return p->const_value[i]; |
|
| 202 |
+ d->type = e_const; |
|
| 203 |
+ d->a.const_index = i; |
|
| 204 |
+ return d; |
|
| 146 | 205 |
} |
| 147 | 206 |
} |
| 148 | 207 |
|
| ... | ... |
@@ -150,126 +208,198 @@ static double evalPrimary(Parser *p){
|
| 150 | 150 |
if(p->s==NULL){
|
| 151 | 151 |
*p->error = "missing (";
|
| 152 | 152 |
p->s= next; |
| 153 |
- return NAN; |
|
| 153 |
+ ff_eval_free(d); |
|
| 154 |
+ return NULL; |
|
| 154 | 155 |
} |
| 155 | 156 |
p->s++; // "("
|
| 156 |
- d= evalExpression(p); |
|
| 157 |
+ if (*next == '(') { // special case do-nothing
|
|
| 158 |
+ av_freep(&d); |
|
| 159 |
+ d = parse_expr(p); |
|
| 160 |
+ if(p->s[0] != ')'){
|
|
| 161 |
+ *p->error = "missing )"; |
|
| 162 |
+ ff_eval_free(d); |
|
| 163 |
+ return NULL; |
|
| 164 |
+ } |
|
| 165 |
+ p->s++; // ")" |
|
| 166 |
+ return d; |
|
| 167 |
+ } |
|
| 168 |
+ d->param[0] = parse_expr(p); |
|
| 157 | 169 |
if(p->s[0]== ','){
|
| 158 | 170 |
p->s++; // "," |
| 159 |
- d2= evalExpression(p); |
|
| 171 |
+ d->param[1] = parse_expr(p); |
|
| 160 | 172 |
} |
| 161 | 173 |
if(p->s[0] != ')'){
|
| 162 | 174 |
*p->error = "missing )"; |
| 163 |
- return NAN; |
|
| 175 |
+ ff_eval_free(d); |
|
| 176 |
+ return NULL; |
|
| 164 | 177 |
} |
| 165 | 178 |
p->s++; // ")" |
| 166 | 179 |
|
| 167 |
- if( strmatch(next, "sinh" ) ) d= sinh(d); |
|
| 168 |
- else if( strmatch(next, "cosh" ) ) d= cosh(d); |
|
| 169 |
- else if( strmatch(next, "tanh" ) ) d= tanh(d); |
|
| 170 |
- else if( strmatch(next, "sin" ) ) d= sin(d); |
|
| 171 |
- else if( strmatch(next, "cos" ) ) d= cos(d); |
|
| 172 |
- else if( strmatch(next, "tan" ) ) d= tan(d); |
|
| 173 |
- else if( strmatch(next, "atan" ) ) d= atan(d); |
|
| 174 |
- else if( strmatch(next, "asin" ) ) d= asin(d); |
|
| 175 |
- else if( strmatch(next, "acos" ) ) d= acos(d); |
|
| 176 |
- else if( strmatch(next, "exp" ) ) d= exp(d); |
|
| 177 |
- else if( strmatch(next, "log" ) ) d= log(d); |
|
| 178 |
- else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d)); |
|
| 179 |
- else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI); |
|
| 180 |
- else if( strmatch(next, "abs" ) ) d= fabs(d); |
|
| 181 |
- else if( strmatch(next, "mod" ) ) d-= floor(d/d2)*d2; |
|
| 182 |
- else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2; |
|
| 183 |
- else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2; |
|
| 184 |
- else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0; |
|
| 185 |
- else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0; |
|
| 186 |
- else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0; |
|
| 187 |
- else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0; |
|
| 188 |
- else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0; |
|
| 189 |
- else if( strmatch(next, "(" ) ) d= d;
|
|
| 190 |
-// else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1); |
|
| 191 |
-// else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0; |
|
| 192 |
- else{
|
|
| 180 |
+ d->type = e_func0; |
|
| 181 |
+ if( strmatch(next, "sinh" ) ) d->a.func0 = sinh; |
|
| 182 |
+ else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh; |
|
| 183 |
+ else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh; |
|
| 184 |
+ else if( strmatch(next, "sin" ) ) d->a.func0 = sin; |
|
| 185 |
+ else if( strmatch(next, "cos" ) ) d->a.func0 = cos; |
|
| 186 |
+ else if( strmatch(next, "tan" ) ) d->a.func0 = tan; |
|
| 187 |
+ else if( strmatch(next, "atan" ) ) d->a.func0 = atan; |
|
| 188 |
+ else if( strmatch(next, "asin" ) ) d->a.func0 = asin; |
|
| 189 |
+ else if( strmatch(next, "acos" ) ) d->a.func0 = acos; |
|
| 190 |
+ else if( strmatch(next, "exp" ) ) d->a.func0 = exp; |
|
| 191 |
+ else if( strmatch(next, "log" ) ) d->a.func0 = log; |
|
| 192 |
+ else if( strmatch(next, "abs" ) ) d->a.func0 = fabs; |
|
| 193 |
+ else if( strmatch(next, "squish") ) d->type = e_squish; |
|
| 194 |
+ else if( strmatch(next, "gauss" ) ) d->type = e_gauss; |
|
| 195 |
+ else if( strmatch(next, "mod" ) ) d->type = e_mod; |
|
| 196 |
+ else if( strmatch(next, "max" ) ) d->type = e_max; |
|
| 197 |
+ else if( strmatch(next, "min" ) ) d->type = e_min; |
|
| 198 |
+ else if( strmatch(next, "eq" ) ) d->type = e_eq; |
|
| 199 |
+ else if( strmatch(next, "gt" ) ) d->type = e_gt; |
|
| 200 |
+ else if( strmatch(next, "gte" ) ) d->type = e_gte; |
|
| 201 |
+ else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
|
|
| 202 |
+ else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
|
|
| 203 |
+ else {
|
|
| 193 | 204 |
for(i=0; p->func1_name && p->func1_name[i]; i++){
|
| 194 | 205 |
if(strmatch(next, p->func1_name[i])){
|
| 195 |
- return p->func1[i](p->opaque, d); |
|
| 206 |
+ d->a.func1 = p->func1[i]; |
|
| 207 |
+ d->type = e_func1; |
|
| 208 |
+ return d; |
|
| 196 | 209 |
} |
| 197 | 210 |
} |
| 198 | 211 |
|
| 199 | 212 |
for(i=0; p->func2_name && p->func2_name[i]; i++){
|
| 200 | 213 |
if(strmatch(next, p->func2_name[i])){
|
| 201 |
- return p->func2[i](p->opaque, d, d2); |
|
| 214 |
+ d->a.func2 = p->func2[i]; |
|
| 215 |
+ d->type = e_func2; |
|
| 216 |
+ return d; |
|
| 202 | 217 |
} |
| 203 | 218 |
} |
| 204 | 219 |
|
| 205 | 220 |
*p->error = "unknown function"; |
| 206 |
- return NAN; |
|
| 221 |
+ ff_eval_free(d); |
|
| 222 |
+ return NULL; |
|
| 207 | 223 |
} |
| 208 | 224 |
|
| 209 | 225 |
return d; |
| 210 | 226 |
} |
| 211 | 227 |
|
| 212 |
-static double evalPow(Parser *p, int *sign){
|
|
| 228 |
+static AVEvalExpr * parse_pow(Parser *p, int *sign){
|
|
| 213 | 229 |
*sign= (*p->s == '+') - (*p->s == '-'); |
| 214 | 230 |
p->s += *sign&1; |
| 215 |
- return evalPrimary(p); |
|
| 231 |
+ return parse_primary(p); |
|
| 216 | 232 |
} |
| 217 | 233 |
|
| 218 |
-static double evalFactor(Parser *p){
|
|
| 234 |
+static AVEvalExpr * parse_factor(Parser *p){
|
|
| 219 | 235 |
int sign, sign2; |
| 220 |
- double ret, e; |
|
| 221 |
- ret= evalPow(p, &sign); |
|
| 236 |
+ AVEvalExpr * e = parse_pow(p, &sign); |
|
| 222 | 237 |
while(p->s[0]=='^'){
|
| 238 |
+ AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr)); |
|
| 239 |
+ |
|
| 223 | 240 |
p->s++; |
| 224 |
- e= evalPow(p, &sign2); |
|
| 225 |
- ret= pow(ret, (sign2|1) * e); |
|
| 241 |
+ |
|
| 242 |
+ tmp->type = e_pow; |
|
| 243 |
+ tmp->value = 1.; |
|
| 244 |
+ tmp->param[0] = e; |
|
| 245 |
+ tmp->param[1] = parse_pow(p, &sign2); |
|
| 246 |
+ if (tmp->param[1]) tmp->param[1]->value *= (sign2|1); |
|
| 247 |
+ e = tmp; |
|
| 226 | 248 |
} |
| 227 |
- return (sign|1) * ret; |
|
| 249 |
+ if (e) e->value *= (sign|1); |
|
| 250 |
+ return e; |
|
| 228 | 251 |
} |
| 229 | 252 |
|
| 230 |
-static double evalTerm(Parser *p){
|
|
| 231 |
- double ret= evalFactor(p); |
|
| 253 |
+static AVEvalExpr * parse_term(Parser *p){
|
|
| 254 |
+ AVEvalExpr * e = parse_factor(p); |
|
| 232 | 255 |
while(p->s[0]=='*' || p->s[0]=='/'){
|
| 233 |
- if(*p->s++ == '*') ret*= evalFactor(p); |
|
| 234 |
- else ret/= evalFactor(p); |
|
| 256 |
+ AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr)); |
|
| 257 |
+ if(*p->s++ == '*') tmp->type = e_mul; |
|
| 258 |
+ else tmp->type = e_div; |
|
| 259 |
+ tmp->value = 1.; |
|
| 260 |
+ tmp->param[0] = e; |
|
| 261 |
+ tmp->param[1] = parse_factor(p); |
|
| 262 |
+ e = tmp; |
|
| 235 | 263 |
} |
| 236 |
- return ret; |
|
| 264 |
+ return e; |
|
| 237 | 265 |
} |
| 238 | 266 |
|
| 239 |
-static double evalExpression(Parser *p){
|
|
| 240 |
- double ret= 0; |
|
| 267 |
+static AVEvalExpr * parse_expr(Parser *p) {
|
|
| 268 |
+ AVEvalExpr * e; |
|
| 241 | 269 |
|
| 242 | 270 |
if(p->stack_index <= 0) //protect against stack overflows |
| 243 |
- return NAN; |
|
| 271 |
+ return NULL; |
|
| 244 | 272 |
p->stack_index--; |
| 245 | 273 |
|
| 246 |
- do{
|
|
| 247 |
- ret += evalTerm(p); |
|
| 248 |
- }while(*p->s == '+' || *p->s == '-'); |
|
| 274 |
+ e = parse_term(p); |
|
| 275 |
+ |
|
| 276 |
+ while(*p->s == '+' || *p->s == '-') {
|
|
| 277 |
+ AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr)); |
|
| 278 |
+ tmp->type = e_add; |
|
| 279 |
+ tmp->value = 1.; |
|
| 280 |
+ tmp->param[0] = e; |
|
| 281 |
+ tmp->param[1] = parse_term(p); |
|
| 282 |
+ e = tmp; |
|
| 283 |
+ }; |
|
| 249 | 284 |
|
| 250 | 285 |
p->stack_index++; |
| 251 | 286 |
|
| 252 |
- return ret; |
|
| 287 |
+ return e; |
|
| 253 | 288 |
} |
| 254 | 289 |
|
| 255 |
-double ff_eval2(char *s, double *const_value, const char **const_name, |
|
| 290 |
+static int verify_expr(AVEvalExpr * e) {
|
|
| 291 |
+ if (!e) return 0; |
|
| 292 |
+ switch (e->type) {
|
|
| 293 |
+ case e_value: |
|
| 294 |
+ case e_const: return 1; |
|
| 295 |
+ case e_func0: |
|
| 296 |
+ case e_func1: |
|
| 297 |
+ case e_squish: |
|
| 298 |
+ case e_gauss: return verify_expr(e->param[0]); |
|
| 299 |
+ default: return verify_expr(e->param[0]) && verify_expr(e->param[1]); |
|
| 300 |
+ } |
|
| 301 |
+} |
|
| 302 |
+ |
|
| 303 |
+AVEvalExpr * ff_parse(char *s, const char **const_name, |
|
| 256 | 304 |
double (**func1)(void *, double), const char **func1_name, |
| 257 | 305 |
double (**func2)(void *, double, double), char **func2_name, |
| 258 |
- void *opaque, char **error){
|
|
| 306 |
+ char **error){
|
|
| 259 | 307 |
Parser p; |
| 308 |
+ AVEvalExpr * e; |
|
| 260 | 309 |
|
| 261 | 310 |
p.stack_index=100; |
| 262 | 311 |
p.s= s; |
| 263 |
- p.const_value= const_value; |
|
| 264 | 312 |
p.const_name = const_name; |
| 265 | 313 |
p.func1 = func1; |
| 266 | 314 |
p.func1_name = func1_name; |
| 267 | 315 |
p.func2 = func2; |
| 268 | 316 |
p.func2_name = func2_name; |
| 269 |
- p.opaque = opaque; |
|
| 270 | 317 |
p.error= error; |
| 271 | 318 |
|
| 272 |
- return evalExpression(&p); |
|
| 319 |
+ e = parse_expr(&p); |
|
| 320 |
+ if (!verify_expr(e)) {
|
|
| 321 |
+ ff_eval_free(e); |
|
| 322 |
+ return NULL; |
|
| 323 |
+ } |
|
| 324 |
+ return e; |
|
| 325 |
+} |
|
| 326 |
+ |
|
| 327 |
+double ff_parse_eval(AVEvalExpr * e, double *const_value, void *opaque) {
|
|
| 328 |
+ Parser p; |
|
| 329 |
+ |
|
| 330 |
+ p.const_value= const_value; |
|
| 331 |
+ p.opaque = opaque; |
|
| 332 |
+ return eval_expr(&p, e); |
|
| 333 |
+} |
|
| 334 |
+ |
|
| 335 |
+double ff_eval2(char *s, double *const_value, const char **const_name, |
|
| 336 |
+ double (**func1)(void *, double), const char **func1_name, |
|
| 337 |
+ double (**func2)(void *, double, double), char **func2_name, |
|
| 338 |
+ void *opaque, char **error){
|
|
| 339 |
+ AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error); |
|
| 340 |
+ double d; |
|
| 341 |
+ if (!e) return NAN; |
|
| 342 |
+ d = ff_parse_eval(e, const_value, opaque); |
|
| 343 |
+ ff_eval_free(e); |
|
| 344 |
+ return d; |
|
| 273 | 345 |
} |
| 274 | 346 |
|
| 275 | 347 |
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) |
| ... | ... |
@@ -39,4 +39,12 @@ double ff_eval2(char *s, double *const_value, const char **const_name, |
| 39 | 39 |
double (**func2)(void *, double, double), char **func2_name, |
| 40 | 40 |
void *opaque, char **error); |
| 41 | 41 |
|
| 42 |
+typedef struct ff_expr_s AVEvalExpr; |
|
| 43 |
+AVEvalExpr * ff_parse(char *s, const char **const_name, |
|
| 44 |
+ double (**func1)(void *, double), const char **func1_name, |
|
| 45 |
+ double (**func2)(void *, double, double), char **func2_name, |
|
| 46 |
+ char **error); |
|
| 47 |
+double ff_parse_eval(AVEvalExpr * e, double *const_value, void *opaque); |
|
| 48 |
+void ff_eval_free(AVEvalExpr * e); |
|
| 49 |
+ |
|
| 42 | 50 |
#endif /* AVCODEC_EVAL_H */ |