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.

247 lines
6.8KB

  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. char **error;
  50. } Parser;
  51. extern double av_strtod(const char *name, char **tail);
  52. static double evalExpression(Parser *p);
  53. static int strmatch(const char *s, const char *prefix){
  54. int i;
  55. for(i=0; prefix[i]; i++){
  56. if(prefix[i] != s[i]) return 0;
  57. }
  58. return 1;
  59. }
  60. static double evalPrimary(Parser *p){
  61. double d, d2=NAN;
  62. char *next= p->s;
  63. int i;
  64. /* number */
  65. d= av_strtod(p->s, &next);
  66. if(next != p->s){
  67. p->s= next;
  68. return d;
  69. }
  70. /* named constants */
  71. for(i=0; p->const_name && p->const_name[i]; i++){
  72. if(strmatch(p->s, p->const_name[i])){
  73. p->s+= strlen(p->const_name[i]);
  74. return p->const_value[i];
  75. }
  76. }
  77. p->s= strchr(p->s, '(');
  78. if(p->s==NULL){
  79. *p->error = "missing (";
  80. p->s= next;
  81. return NAN;
  82. }
  83. p->s++; // "("
  84. d= evalExpression(p);
  85. if(p->s[0]== ','){
  86. p->s++; // ","
  87. d2= evalExpression(p);
  88. }
  89. if(p->s[0] != ')'){
  90. *p->error = "missing )";
  91. return NAN;
  92. }
  93. p->s++; // ")"
  94. if( strmatch(next, "sinh" ) ) d= sinh(d);
  95. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  96. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  97. else if( strmatch(next, "sin" ) ) d= sin(d);
  98. else if( strmatch(next, "cos" ) ) d= cos(d);
  99. else if( strmatch(next, "tan" ) ) d= tan(d);
  100. else if( strmatch(next, "exp" ) ) d= exp(d);
  101. else if( strmatch(next, "log" ) ) d= log(d);
  102. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  103. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  104. else if( strmatch(next, "abs" ) ) d= fabs(d);
  105. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  106. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  107. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  108. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  109. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  110. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  111. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  112. else if( strmatch(next, "(" ) ) d= d;
  113. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  114. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  115. else{
  116. for(i=0; p->func1_name && p->func1_name[i]; i++){
  117. if(strmatch(next, p->func1_name[i])){
  118. return p->func1[i](p->opaque, d);
  119. }
  120. }
  121. for(i=0; p->func2_name && p->func2_name[i]; i++){
  122. if(strmatch(next, p->func2_name[i])){
  123. return p->func2[i](p->opaque, d, d2);
  124. }
  125. }
  126. *p->error = "unknown function";
  127. return NAN;
  128. }
  129. return d;
  130. }
  131. static double evalPow(Parser *p){
  132. int sign= (*p->s == '+') - (*p->s == '-');
  133. p->s += sign&1;
  134. return (sign|1) * evalPrimary(p);
  135. }
  136. static double evalFactor(Parser *p){
  137. double ret= evalPow(p);
  138. while(p->s[0]=='^'){
  139. p->s++;
  140. ret= pow(ret, evalPow(p));
  141. }
  142. return ret;
  143. }
  144. static double evalTerm(Parser *p){
  145. double ret= evalFactor(p);
  146. while(p->s[0]=='*' || p->s[0]=='/'){
  147. if(*p->s++ == '*') ret*= evalFactor(p);
  148. else ret/= evalFactor(p);
  149. }
  150. return ret;
  151. }
  152. static double evalExpression(Parser *p){
  153. double ret= 0;
  154. if(p->stack_index <= 0) //protect against stack overflows
  155. return NAN;
  156. p->stack_index--;
  157. do{
  158. ret += evalTerm(p);
  159. }while(*p->s == '+' || *p->s == '-');
  160. p->stack_index++;
  161. return ret;
  162. }
  163. double ff_eval2(char *s, double *const_value, const char **const_name,
  164. double (**func1)(void *, double), const char **func1_name,
  165. double (**func2)(void *, double, double), char **func2_name,
  166. void *opaque, char **error){
  167. Parser p;
  168. p.stack_index=100;
  169. p.s= s;
  170. p.const_value= const_value;
  171. p.const_name = const_name;
  172. p.func1 = func1;
  173. p.func1_name = func1_name;
  174. p.func2 = func2;
  175. p.func2_name = func2_name;
  176. p.opaque = opaque;
  177. p.error= error;
  178. return evalExpression(&p);
  179. }
  180. #if LIBAVCODEC_VERSION_INT < ((51<<16)+(16<<8)+0)
  181. attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
  182. double (**func1)(void *, double), const char **func1_name,
  183. double (**func2)(void *, double, double), char **func2_name,
  184. void *opaque){
  185. char *error=NULL;
  186. double ret;
  187. ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
  188. if (error)
  189. av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
  190. return ret;
  191. }
  192. #endif
  193. #ifdef TEST
  194. #undef printf
  195. static double const_values[]={
  196. M_PI,
  197. M_E,
  198. 0
  199. };
  200. static const char *const_names[]={
  201. "PI",
  202. "E",
  203. 0
  204. };
  205. main(){
  206. int i;
  207. 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));
  208. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  209. for(i=0; i<1050; i++){
  210. START_TIMER
  211. 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);
  212. STOP_TIMER("ff_eval")
  213. }
  214. }
  215. #endif