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.

256 lines
6.5KB

  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. #define STACK_SIZE 100
  40. typedef struct Parser{
  41. double stack[STACK_SIZE];
  42. int stack_index;
  43. char *s;
  44. double *const_value;
  45. const char **const_name; // NULL terminated
  46. double (**func1)(void *, double a); // NULL terminated
  47. const char **func1_name; // NULL terminated
  48. double (**func2)(void *, double a, double b); // NULL terminated
  49. char **func2_name; // NULL terminated
  50. void *opaque;
  51. } Parser;
  52. static void evalExpression(Parser *p);
  53. static void push(Parser *p, double d){
  54. if(p->stack_index+1>= STACK_SIZE){
  55. av_log(NULL, AV_LOG_ERROR, "stack overflow in the parser\n");
  56. return;
  57. }
  58. p->stack[ p->stack_index++ ]= d;
  59. //printf("push %f\n", d); fflush(stdout);
  60. }
  61. static double pop(Parser *p){
  62. if(p->stack_index<=0){
  63. av_log(NULL, AV_LOG_ERROR, "stack underflow in the parser\n");
  64. return NAN;
  65. }
  66. //printf("pop\n"); fflush(stdout);
  67. return p->stack[ --p->stack_index ];
  68. }
  69. static int strmatch(const char *s, const char *prefix){
  70. int i;
  71. for(i=0; prefix[i]; i++){
  72. if(prefix[i] != s[i]) return 0;
  73. }
  74. return 1;
  75. }
  76. static void evalPrimary(Parser *p){
  77. double d, d2=NAN;
  78. char *next= p->s;
  79. int i;
  80. /* number */
  81. d= strtod(p->s, &next);
  82. if(next != p->s){
  83. push(p, d);
  84. p->s= next;
  85. return;
  86. }
  87. /* named constants */
  88. for(i=0; p->const_name[i]; i++){
  89. if(strmatch(p->s, p->const_name[i])){
  90. push(p, p->const_value[i]);
  91. p->s+= strlen(p->const_name[i]);
  92. return;
  93. }
  94. }
  95. p->s= strchr(p->s, '(');
  96. if(p->s==NULL){
  97. av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
  98. return;
  99. }
  100. p->s++; // "("
  101. evalExpression(p);
  102. d= pop(p);
  103. p->s++; // ")" or ","
  104. if(p->s[-1]== ','){
  105. evalExpression(p);
  106. d2= pop(p);
  107. p->s++; // ")"
  108. }
  109. if( strmatch(next, "sinh" ) ) d= sinh(d);
  110. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  111. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  112. else if( strmatch(next, "sin" ) ) d= sin(d);
  113. else if( strmatch(next, "cos" ) ) d= cos(d);
  114. else if( strmatch(next, "tan" ) ) d= tan(d);
  115. else if( strmatch(next, "exp" ) ) d= exp(d);
  116. else if( strmatch(next, "log" ) ) d= log(d);
  117. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  118. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  119. else if( strmatch(next, "abs" ) ) d= fabs(d);
  120. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  121. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  122. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  123. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  124. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  125. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  126. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  127. else{
  128. int error=1;
  129. for(i=0; p->func1_name && p->func1_name[i]; i++){
  130. if(strmatch(next, p->func1_name[i])){
  131. d= p->func1[i](p->opaque, d);
  132. error=0;
  133. break;
  134. }
  135. }
  136. for(i=0; p->func2_name && p->func2_name[i]; i++){
  137. if(strmatch(next, p->func2_name[i])){
  138. d= p->func2[i](p->opaque, d, d2);
  139. error=0;
  140. break;
  141. }
  142. }
  143. if(error){
  144. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  145. return;
  146. }
  147. }
  148. if(p->s[-1]!= ')'){
  149. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  150. return;
  151. }
  152. push(p, d);
  153. }
  154. static void evalPow(Parser *p){
  155. int neg= 0;
  156. if(p->s[0]=='+') p->s++;
  157. if(p->s[0]=='-'){
  158. neg= 1;
  159. p->s++;
  160. }
  161. if(p->s[0]=='('){
  162. p->s++;;
  163. evalExpression(p);
  164. if(p->s[0]!=')')
  165. av_log(NULL, AV_LOG_ERROR, "Parser: missing )\n");
  166. p->s++;
  167. }else{
  168. evalPrimary(p);
  169. }
  170. if(neg) push(p, -pop(p));
  171. }
  172. static void evalFactor(Parser *p){
  173. evalPow(p);
  174. while(p->s[0]=='^'){
  175. double d;
  176. p->s++;
  177. evalPow(p);
  178. d= pop(p);
  179. push(p, pow(pop(p), d));
  180. }
  181. }
  182. static void evalTerm(Parser *p){
  183. evalFactor(p);
  184. while(p->s[0]=='*' || p->s[0]=='/'){
  185. int inv= p->s[0]=='/';
  186. double d;
  187. p->s++;
  188. evalFactor(p);
  189. d= pop(p);
  190. if(inv) d= 1.0/d;
  191. push(p, d * pop(p));
  192. }
  193. }
  194. static void evalExpression(Parser *p){
  195. evalTerm(p);
  196. while(p->s[0]=='+' || p->s[0]=='-'){
  197. int sign= p->s[0]=='-';
  198. double d;
  199. p->s++;
  200. evalTerm(p);
  201. d= pop(p);
  202. if(sign) d= -d;
  203. push(p, d + pop(p));
  204. }
  205. }
  206. double ff_eval(char *s, double *const_value, const char **const_name,
  207. double (**func1)(void *, double), const char **func1_name,
  208. double (**func2)(void *, double, double), char **func2_name,
  209. void *opaque){
  210. Parser p;
  211. p.stack_index=0;
  212. p.s= s;
  213. p.const_value= const_value;
  214. p.const_name = const_name;
  215. p.func1 = func1;
  216. p.func1_name = func1_name;
  217. p.func2 = func2;
  218. p.func2_name = func2_name;
  219. p.opaque = opaque;
  220. evalExpression(&p);
  221. return pop(&p);
  222. }