You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.2KB

  1. /*
  2. * simple arithmetic expression evaluator
  3. *
  4. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /**
  22. * @file eval.c
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <math.h>
  33. #ifndef NAN
  34. #define NAN 0
  35. #endif
  36. #ifndef M_PI
  37. #define M_PI 3.14159265358979323846
  38. #endif
  39. typedef struct Parser{
  40. int stack_index;
  41. char *s;
  42. double *const_value;
  43. const char **const_name; // NULL terminated
  44. double (**func1)(void *, double a); // NULL terminated
  45. const char **func1_name; // NULL terminated
  46. double (**func2)(void *, double a, double b); // NULL terminated
  47. char **func2_name; // NULL terminated
  48. void *opaque;
  49. } Parser;
  50. static double evalExpression(Parser *p);
  51. static int strmatch(const char *s, const char *prefix){
  52. int i;
  53. for(i=0; prefix[i]; i++){
  54. if(prefix[i] != s[i]) return 0;
  55. }
  56. return 1;
  57. }
  58. static double evalPrimary(Parser *p){
  59. double d, d2=NAN;
  60. char *next= p->s;
  61. int i;
  62. /* number */
  63. d= strtod(p->s, &next);
  64. if(next != p->s){
  65. p->s= next;
  66. return d;
  67. }
  68. /* named constants */
  69. for(i=0; p->const_name && p->const_name[i]; i++){
  70. if(strmatch(p->s, p->const_name[i])){
  71. p->s+= strlen(p->const_name[i]);
  72. return p->const_value[i];
  73. }
  74. }
  75. p->s= strchr(p->s, '(');
  76. if(p->s==NULL){
  77. av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
  78. return NAN;
  79. }
  80. p->s++; // "("
  81. d= evalExpression(p);
  82. if(p->s[0]== ','){
  83. p->s++; // ","
  84. d2= evalExpression(p);
  85. }
  86. if(p->s[0] != ')'){
  87. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  88. return NAN;
  89. }
  90. p->s++; // ")"
  91. if( strmatch(next, "sinh" ) ) d= sinh(d);
  92. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  93. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  94. else if( strmatch(next, "sin" ) ) d= sin(d);
  95. else if( strmatch(next, "cos" ) ) d= cos(d);
  96. else if( strmatch(next, "tan" ) ) d= tan(d);
  97. else if( strmatch(next, "exp" ) ) d= exp(d);
  98. else if( strmatch(next, "log" ) ) d= log(d);
  99. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  100. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  101. else if( strmatch(next, "abs" ) ) d= fabs(d);
  102. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  103. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  104. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  105. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  106. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  107. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  108. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  109. else if( strmatch(next, "(" ) ) d= d;
  110. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  111. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  112. else{
  113. for(i=0; p->func1_name && p->func1_name[i]; i++){
  114. if(strmatch(next, p->func1_name[i])){
  115. return p->func1[i](p->opaque, d);
  116. }
  117. }
  118. for(i=0; p->func2_name && p->func2_name[i]; i++){
  119. if(strmatch(next, p->func2_name[i])){
  120. return p->func2[i](p->opaque, d, d2);
  121. }
  122. }
  123. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  124. return NAN;
  125. }
  126. return d;
  127. }
  128. static double evalPow(Parser *p){
  129. int sign= (*p->s == '+') - (*p->s == '-');
  130. p->s += sign&1;
  131. return (sign|1) * evalPrimary(p);
  132. }
  133. static double evalFactor(Parser *p){
  134. double ret= evalPow(p);
  135. while(p->s[0]=='^'){
  136. p->s++;
  137. ret= pow(ret, evalPow(p));
  138. }
  139. return ret;
  140. }
  141. static double evalTerm(Parser *p){
  142. double ret= evalFactor(p);
  143. while(p->s[0]=='*' || p->s[0]=='/'){
  144. if(*p->s++ == '*') ret*= evalFactor(p);
  145. else ret/= evalFactor(p);
  146. }
  147. return ret;
  148. }
  149. static double evalExpression(Parser *p){
  150. double ret= 0;
  151. if(p->stack_index <= 0) //protect against stack overflows
  152. return NAN;
  153. p->stack_index--;
  154. do{
  155. ret += evalTerm(p);
  156. }while(*p->s == '+' || *p->s == '-');
  157. p->stack_index++;
  158. return ret;
  159. }
  160. double ff_eval(char *s, double *const_value, const char **const_name,
  161. double (**func1)(void *, double), const char **func1_name,
  162. double (**func2)(void *, double, double), char **func2_name,
  163. void *opaque){
  164. Parser p;
  165. p.stack_index=100;
  166. p.s= s;
  167. p.const_value= const_value;
  168. p.const_name = const_name;
  169. p.func1 = func1;
  170. p.func1_name = func1_name;
  171. p.func2 = func2;
  172. p.func2_name = func2_name;
  173. p.opaque = opaque;
  174. return evalExpression(&p);
  175. }
  176. #ifdef TEST
  177. #undef printf
  178. static double const_values[]={
  179. M_PI,
  180. M_E,
  181. 0
  182. };
  183. static const char *const_names[]={
  184. "PI",
  185. "E",
  186. 0
  187. };
  188. main(){
  189. int i;
  190. 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));
  191. for(i=0; i<1050; i++){
  192. START_TIMER
  193. 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);
  194. STOP_TIMER("ff_eval")
  195. }
  196. }
  197. #endif