| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at> | 2 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at> |
| 3 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org> | 3 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org> |
| 4 * | 4 * |
| 5 * This file is part of FFmpeg. | 5 * This file is part of FFmpeg. |
| 6 * | 6 * |
| 7 * FFmpeg is free software; you can redistribute it and/or | 7 * FFmpeg is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Lesser General Public | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2.1 of the License, or (at your option) any later version. | 10 * version 2.1 of the License, or (at your option) any later version. |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 { | 175 { |
| 176 if (!e) return; | 176 if (!e) return; |
| 177 av_free_expr(e->param[0]); | 177 av_free_expr(e->param[0]); |
| 178 av_free_expr(e->param[1]); | 178 av_free_expr(e->param[1]); |
| 179 av_freep(&e); | 179 av_freep(&e); |
| 180 } | 180 } |
| 181 | 181 |
| 182 static int parse_primary(AVExpr **e, Parser *p) | 182 static int parse_primary(AVExpr **e, Parser *p) |
| 183 { | 183 { |
| 184 AVExpr *d = av_mallocz(sizeof(AVExpr)); | 184 AVExpr *d = av_mallocz(sizeof(AVExpr)); |
| 185 char *next= p->s; | 185 char *next = p->s, *s0 = p->s; |
| 186 int ret, i; | 186 int ret, i; |
| 187 | 187 |
| 188 if (!d) | 188 if (!d) |
| 189 return AVERROR(ENOMEM); | 189 return AVERROR(ENOMEM); |
| 190 | 190 |
| 191 /* number */ | 191 /* number */ |
| 192 d->value = av_strtod(p->s, &next); | 192 d->value = av_strtod(p->s, &next); |
| 193 if (next != p->s) { | 193 if (next != p->s) { |
| 194 d->type = e_value; | 194 d->type = e_value; |
| 195 p->s= next; | 195 p->s= next; |
| 196 *e = d; | 196 *e = d; |
| 197 return 0; | 197 return 0; |
| 198 } | 198 } |
| 199 d->value = 1; | 199 d->value = 1; |
| 200 | 200 |
| 201 /* named constants */ | 201 /* named constants */ |
| 202 for (i=0; p->const_names && p->const_names[i]; i++) { | 202 for (i=0; p->const_names && p->const_names[i]; i++) { |
| 203 if (strmatch(p->s, p->const_names[i])) { | 203 if (strmatch(p->s, p->const_names[i])) { |
| 204 p->s+= strlen(p->const_names[i]); | 204 p->s+= strlen(p->const_names[i]); |
| 205 d->type = e_const; | 205 d->type = e_const; |
| 206 d->a.const_index = i; | 206 d->a.const_index = i; |
| 207 *e = d; | 207 *e = d; |
| 208 return 0; | 208 return 0; |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 | 211 |
| 212 p->s= strchr(p->s, '('); | 212 p->s= strchr(p->s, '('); |
| 213 if (p->s==NULL) { | 213 if (p->s==NULL) { |
| 214 av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n"); | 214 av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s
0); |
| 215 p->s= next; | 215 p->s= next; |
| 216 av_free_expr(d); | 216 av_free_expr(d); |
| 217 return AVERROR(EINVAL); | 217 return AVERROR(EINVAL); |
| 218 } | 218 } |
| 219 p->s++; // "(" | 219 p->s++; // "(" |
| 220 if (*next == '(') { // special case do-nothing | 220 if (*next == '(') { // special case do-nothing |
| 221 av_freep(&d); | 221 av_freep(&d); |
| 222 if ((ret = parse_expr(&d, p)) < 0) | 222 if ((ret = parse_expr(&d, p)) < 0) |
| 223 return ret; | 223 return ret; |
| 224 if (p->s[0] != ')') { | 224 if (p->s[0] != ')') { |
| 225 av_log(p, AV_LOG_ERROR, "missing )\n"); | 225 av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0); |
| 226 av_free_expr(d); | 226 av_free_expr(d); |
| 227 return AVERROR(EINVAL); | 227 return AVERROR(EINVAL); |
| 228 } | 228 } |
| 229 p->s++; // ")" | 229 p->s++; // ")" |
| 230 *e = d; | 230 *e = d; |
| 231 return 0; | 231 return 0; |
| 232 } | 232 } |
| 233 if ((ret = parse_expr(&(d->param[0]), p)) < 0) { | 233 if ((ret = parse_expr(&(d->param[0]), p)) < 0) { |
| 234 av_free_expr(d); | 234 av_free_expr(d); |
| 235 return ret; | 235 return ret; |
| 236 } | 236 } |
| 237 if (p->s[0]== ',') { | 237 if (p->s[0]== ',') { |
| 238 p->s++; // "," | 238 p->s++; // "," |
| 239 parse_expr(&d->param[1], p); | 239 parse_expr(&d->param[1], p); |
| 240 } | 240 } |
| 241 if (p->s[0] != ')') { | 241 if (p->s[0] != ')') { |
| 242 av_log(p, AV_LOG_ERROR, "missing )\n"); | 242 av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0); |
| 243 av_free_expr(d); | 243 av_free_expr(d); |
| 244 return AVERROR(EINVAL); | 244 return AVERROR(EINVAL); |
| 245 } | 245 } |
| 246 p->s++; // ")" | 246 p->s++; // ")" |
| 247 | 247 |
| 248 d->type = e_func0; | 248 d->type = e_func0; |
| 249 if (strmatch(next, "sinh" )) d->a.func0 = sinh; | 249 if (strmatch(next, "sinh" )) d->a.func0 = sinh; |
| 250 else if (strmatch(next, "cosh" )) d->a.func0 = cosh; | 250 else if (strmatch(next, "cosh" )) d->a.func0 = cosh; |
| 251 else if (strmatch(next, "tanh" )) d->a.func0 = tanh; | 251 else if (strmatch(next, "tanh" )) d->a.func0 = tanh; |
| 252 else if (strmatch(next, "sin" )) d->a.func0 = sin; | 252 else if (strmatch(next, "sin" )) d->a.func0 = sin; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 283 | 283 |
| 284 for (i=0; p->func2_names && p->func2_names[i]; i++) { | 284 for (i=0; p->func2_names && p->func2_names[i]; i++) { |
| 285 if (strmatch(next, p->func2_names[i])) { | 285 if (strmatch(next, p->func2_names[i])) { |
| 286 d->a.func2 = p->funcs2[i]; | 286 d->a.func2 = p->funcs2[i]; |
| 287 d->type = e_func2; | 287 d->type = e_func2; |
| 288 *e = d; | 288 *e = d; |
| 289 return 0; | 289 return 0; |
| 290 } | 290 } |
| 291 } | 291 } |
| 292 | 292 |
| 293 av_log(p, AV_LOG_ERROR, "unknown function\n"); | 293 av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0); |
| 294 av_free_expr(d); | 294 av_free_expr(d); |
| 295 return AVERROR(EINVAL); | 295 return AVERROR(EINVAL); |
| 296 } | 296 } |
| 297 | 297 |
| 298 *e = d; | 298 *e = d; |
| 299 return 0; | 299 return 0; |
| 300 } | 300 } |
| 301 | 301 |
| 302 static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1) | 302 static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1) |
| 303 { | 303 { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 int av_parse_expr(AVExpr **expr, const char *s, | 441 int av_parse_expr(AVExpr **expr, const char *s, |
| 442 const char * const *const_names, | 442 const char * const *const_names, |
| 443 const char * const *func1_names, double (* const *funcs1)(void
*, double), | 443 const char * const *func1_names, double (* const *funcs1)(void
*, double), |
| 444 const char * const *func2_names, double (* const *funcs2)(void
*, double, double), | 444 const char * const *func2_names, double (* const *funcs2)(void
*, double, double), |
| 445 int log_offset, void *log_ctx) | 445 int log_offset, void *log_ctx) |
| 446 { | 446 { |
| 447 Parser p; | 447 Parser p; |
| 448 AVExpr *e = NULL; | 448 AVExpr *e = NULL; |
| 449 char *w = av_malloc(strlen(s) + 1); | 449 char *w = av_malloc(strlen(s) + 1); |
| 450 char *wp = w; | 450 char *wp = w; |
| 451 const char *s0 = s; |
| 451 int ret = 0; | 452 int ret = 0; |
| 452 | 453 |
| 453 if (!w) | 454 if (!w) |
| 454 return AVERROR(ENOMEM); | 455 return AVERROR(ENOMEM); |
| 455 | 456 |
| 456 while (*s) | 457 while (*s) |
| 457 if (!isspace(*s++)) *wp++ = s[-1]; | 458 if (!isspace(*s++)) *wp++ = s[-1]; |
| 458 *wp++ = 0; | 459 *wp++ = 0; |
| 459 | 460 |
| 460 p.class = &class; | 461 p.class = &class; |
| 461 p.stack_index=100; | 462 p.stack_index=100; |
| 462 p.s= w; | 463 p.s= w; |
| 463 p.const_names = const_names; | 464 p.const_names = const_names; |
| 464 p.funcs1 = funcs1; | 465 p.funcs1 = funcs1; |
| 465 p.func1_names = func1_names; | 466 p.func1_names = func1_names; |
| 466 p.funcs2 = funcs2; | 467 p.funcs2 = funcs2; |
| 467 p.func2_names = func2_names; | 468 p.func2_names = func2_names; |
| 468 p.log_offset = log_offset; | 469 p.log_offset = log_offset; |
| 469 p.log_ctx = log_ctx; | 470 p.log_ctx = log_ctx; |
| 470 | 471 |
| 471 if ((ret = parse_expr(&e, &p)) < 0) | 472 if ((ret = parse_expr(&e, &p)) < 0) |
| 472 goto end; | 473 goto end; |
| 474 if (*p.s) { |
| 475 av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%
s'\n", p.s, s0); |
| 476 ret = AVERROR(EINVAL); |
| 477 goto end; |
| 478 } |
| 473 if (!verify_expr(e)) { | 479 if (!verify_expr(e)) { |
| 474 av_free_expr(e); | 480 av_free_expr(e); |
| 475 ret = AVERROR(EINVAL); | 481 ret = AVERROR(EINVAL); |
| 476 goto end; | 482 goto end; |
| 477 } | 483 } |
| 478 *expr = e; | 484 *expr = e; |
| 479 end: | 485 end: |
| 480 av_free(w); | 486 av_free(w); |
| 481 return ret; | 487 return ret; |
| 482 } | 488 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 static const char *const_names[] = { | 525 static const char *const_names[] = { |
| 520 "PI", | 526 "PI", |
| 521 "E", | 527 "E", |
| 522 0 | 528 0 |
| 523 }; | 529 }; |
| 524 | 530 |
| 525 int main(void) | 531 int main(void) |
| 526 { | 532 { |
| 527 int i; | 533 int i; |
| 528 double d; | 534 double d; |
| 535 const char **expr, *exprs[] = { |
| 536 "", |
| 537 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", |
| 538 "80G/80Gi" |
| 539 "1k", |
| 540 "1Gi", |
| 541 "1gi", |
| 542 "1GiFoo", |
| 543 "1k+1k", |
| 544 "1Gi*3foo", |
| 545 "foo", |
| 546 "foo(", |
| 547 "foo()", |
| 548 "foo)", |
| 549 "sin", |
| 550 "sin(", |
| 551 "sin()", |
| 552 "sin)", |
| 553 "sin 10", |
| 554 "sin(1,2,3)", |
| 555 "sin(1 )", |
| 556 "1", |
| 557 "1foo", |
| 558 "bar + PI + E + 100f*2 + foo", |
| 559 "13k + 12f - foo(1, 2)", |
| 560 "1gi", |
| 561 "1Gi", |
| 562 NULL |
| 563 }; |
| 564 |
| 565 for (expr = exprs; *expr; expr++) { |
| 566 printf("Evaluating '%s'\n", *expr); |
| 567 av_parse_and_eval_expr(&d, *expr, |
| 568 const_names, const_values, |
| 569 NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 570 printf("'%s' -> %f\n\n", *expr, d); |
| 571 } |
| 572 |
| 529 av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", | 573 av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", |
| 530 const_names, const_values, | 574 const_names, const_values, |
| 531 NULL, NULL, NULL, NULL, NULL, 0, NULL); | 575 NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 532 printf("%f == 12.7\n", d); | 576 printf("%f == 12.7\n", d); |
| 533 av_parse_and_eval_expr(&d, "80G/80Gi", | 577 av_parse_and_eval_expr(&d, "80G/80Gi", |
| 534 const_names, const_values, | 578 const_names, const_values, |
| 535 NULL, NULL, NULL, NULL, NULL, 0, NULL); | 579 NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 536 printf("%f == 0.931322575\n", d); | 580 printf("%f == 0.931322575\n", d); |
| 537 | 581 |
| 538 for (i=0; i<1050; i++) { | 582 for (i=0; i<1050; i++) { |
| 539 START_TIMER | 583 START_TIMER |
| 540 av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)
", | 584 av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)
", |
| 541 const_names, const_values, | 585 const_names, const_values, |
| 542 NULL, NULL, NULL, NULL, NULL, 0, NULL); | 586 NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 543 STOP_TIMER("av_parse_and_eval_expr") | 587 STOP_TIMER("av_parse_and_eval_expr") |
| 544 } | 588 } |
| 545 return 0; | 589 return 0; |
| 546 } | 590 } |
| 547 #endif | 591 #endif |
| OLD | NEW |