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.

229 lines
6.3KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.0/0.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= av_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. p->s= next;
  79. return NAN;
  80. }
  81. p->s++; // "("
  82. d= evalExpression(p);
  83. if(p->s[0]== ','){
  84. p->s++; // ","
  85. d2= evalExpression(p);
  86. }
  87. if(p->s[0] != ')'){
  88. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  89. return NAN;
  90. }
  91. p->s++; // ")"
  92. if( strmatch(next, "sinh" ) ) d= sinh(d);
  93. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  94. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  95. else if( strmatch(next, "sin" ) ) d= sin(d);
  96. else if( strmatch(next, "cos" ) ) d= cos(d);
  97. else if( strmatch(next, "tan" ) ) d= tan(d);
  98. else if( strmatch(next, "exp" ) ) d= exp(d);
  99. else if( strmatch(next, "log" ) ) d= log(d);
  100. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  101. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  102. else if( strmatch(next, "abs" ) ) d= fabs(d);
  103. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  104. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  105. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  106. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  107. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  108. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  109. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  110. else if( strmatch(next, "(" ) ) d= d;
  111. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  112. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  113. else{
  114. for(i=0; p->func1_name && p->func1_name[i]; i++){
  115. if(strmatch(next, p->func1_name[i])){
  116. return p->func1[i](p->opaque, d);
  117. }
  118. }
  119. for(i=0; p->func2_name && p->func2_name[i]; i++){
  120. if(strmatch(next, p->func2_name[i])){
  121. return p->func2[i](p->opaque, d, d2);
  122. }
  123. }
  124. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  125. return NAN;
  126. }
  127. return d;
  128. }
  129. static double evalPow(Parser *p){
  130. int sign= (*p->s == '+') - (*p->s == '-');
  131. p->s += sign&1;
  132. return (sign|1) * evalPrimary(p);
  133. }
  134. static double evalFactor(Parser *p){
  135. double ret= evalPow(p);
  136. while(p->s[0]=='^'){
  137. p->s++;
  138. ret= pow(ret, evalPow(p));
  139. }
  140. return ret;
  141. }
  142. static double evalTerm(Parser *p){
  143. double ret= evalFactor(p);
  144. while(p->s[0]=='*' || p->s[0]=='/'){
  145. if(*p->s++ == '*') ret*= evalFactor(p);
  146. else ret/= evalFactor(p);
  147. }
  148. return ret;
  149. }
  150. static double evalExpression(Parser *p){
  151. double ret= 0;
  152. if(p->stack_index <= 0) //protect against stack overflows
  153. return NAN;
  154. p->stack_index--;
  155. do{
  156. ret += evalTerm(p);
  157. }while(*p->s == '+' || *p->s == '-');
  158. p->stack_index++;
  159. return ret;
  160. }
  161. double ff_eval(char *s, double *const_value, const char **const_name,
  162. double (**func1)(void *, double), const char **func1_name,
  163. double (**func2)(void *, double, double), char **func2_name,
  164. void *opaque){
  165. Parser p;
  166. p.stack_index=100;
  167. p.s= s;
  168. p.const_value= const_value;
  169. p.const_name = const_name;
  170. p.func1 = func1;
  171. p.func1_name = func1_name;
  172. p.func2 = func2;
  173. p.func2_name = func2_name;
  174. p.opaque = opaque;
  175. return evalExpression(&p);
  176. }
  177. #ifdef TEST
  178. #undef printf
  179. static double const_values[]={
  180. M_PI,
  181. M_E,
  182. 0
  183. };
  184. static const char *const_names[]={
  185. "PI",
  186. "E",
  187. 0
  188. };
  189. main(){
  190. int i;
  191. 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));
  192. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  193. for(i=0; i<1050; i++){
  194. START_TIMER
  195. 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);
  196. STOP_TIMER("ff_eval")
  197. }
  198. }
  199. #endif