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.

258 lines
6.6KB

  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. if(p->s[0]== ','){
  104. p->s++; // ","
  105. evalExpression(p);
  106. d2= pop(p);
  107. }
  108. if(p->s[0] != ')'){
  109. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  110. return;
  111. }
  112. p->s++; // ")"
  113. if( strmatch(next, "sinh" ) ) d= sinh(d);
  114. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  115. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  116. else if( strmatch(next, "sin" ) ) d= sin(d);
  117. else if( strmatch(next, "cos" ) ) d= cos(d);
  118. else if( strmatch(next, "tan" ) ) d= tan(d);
  119. else if( strmatch(next, "exp" ) ) d= exp(d);
  120. else if( strmatch(next, "log" ) ) d= log(d);
  121. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  122. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  123. else if( strmatch(next, "abs" ) ) d= fabs(d);
  124. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  125. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  126. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  127. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  128. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  129. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  130. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  131. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  132. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  133. else{
  134. int error=1;
  135. for(i=0; p->func1_name && p->func1_name[i]; i++){
  136. if(strmatch(next, p->func1_name[i])){
  137. d= p->func1[i](p->opaque, d);
  138. error=0;
  139. break;
  140. }
  141. }
  142. for(i=0; p->func2_name && p->func2_name[i]; i++){
  143. if(strmatch(next, p->func2_name[i])){
  144. d= p->func2[i](p->opaque, d, d2);
  145. error=0;
  146. break;
  147. }
  148. }
  149. if(error){
  150. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  151. return;
  152. }
  153. }
  154. push(p, d);
  155. }
  156. static void evalPow(Parser *p){
  157. int neg= 0;
  158. if(p->s[0]=='+') p->s++;
  159. if(p->s[0]=='-'){
  160. neg= 1;
  161. p->s++;
  162. }
  163. if(p->s[0]=='('){
  164. p->s++;;
  165. evalExpression(p);
  166. if(p->s[0]!=')')
  167. av_log(NULL, AV_LOG_ERROR, "Parser: missing )\n");
  168. p->s++;
  169. }else{
  170. evalPrimary(p);
  171. }
  172. if(neg) push(p, -pop(p));
  173. }
  174. static void evalFactor(Parser *p){
  175. evalPow(p);
  176. while(p->s[0]=='^'){
  177. double d;
  178. p->s++;
  179. evalPow(p);
  180. d= pop(p);
  181. push(p, pow(pop(p), d));
  182. }
  183. }
  184. static void evalTerm(Parser *p){
  185. evalFactor(p);
  186. while(p->s[0]=='*' || p->s[0]=='/'){
  187. int inv= p->s[0]=='/';
  188. double d;
  189. p->s++;
  190. evalFactor(p);
  191. d= pop(p);
  192. if(inv) d= 1.0/d;
  193. push(p, d * pop(p));
  194. }
  195. }
  196. static void evalExpression(Parser *p){
  197. evalTerm(p);
  198. while(p->s[0]=='+' || p->s[0]=='-'){
  199. int sign= p->s[0]=='-';
  200. double d;
  201. p->s++;
  202. evalTerm(p);
  203. d= pop(p);
  204. if(sign) d= -d;
  205. push(p, d + pop(p));
  206. }
  207. }
  208. double ff_eval(char *s, double *const_value, const char **const_name,
  209. double (**func1)(void *, double), const char **func1_name,
  210. double (**func2)(void *, double, double), char **func2_name,
  211. void *opaque){
  212. Parser p;
  213. p.stack_index=0;
  214. p.s= s;
  215. p.const_value= const_value;
  216. p.const_name = const_name;
  217. p.func1 = func1;
  218. p.func1_name = func1_name;
  219. p.func2 = func2;
  220. p.func2_name = func2_name;
  221. p.opaque = opaque;
  222. evalExpression(&p);
  223. return pop(&p);
  224. }