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.

310 lines
8.4KB

  1. /*
  2. * simple arithmetic expression evaluator
  3. *
  4. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /**
  24. * @file eval.c
  25. * simple arithmetic expression evaluator.
  26. *
  27. * see http://joe.hotchkiss.com/programming/eval/eval.html
  28. */
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <math.h>
  35. #ifndef NAN
  36. #define NAN 0.0/0.0
  37. #endif
  38. #ifndef M_PI
  39. #define M_PI 3.14159265358979323846
  40. #endif
  41. typedef struct Parser{
  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. char **error;
  52. } Parser;
  53. static double evalExpression(Parser *p);
  54. static int8_t si_prefixes['z' - 'E' + 1]={
  55. ['y'-'E']= -24,
  56. ['z'-'E']= -21,
  57. ['a'-'E']= -18,
  58. ['f'-'E']= -15,
  59. ['p'-'E']= -12,
  60. ['n'-'E']= - 9,
  61. ['u'-'E']= - 6,
  62. ['m'-'E']= - 3,
  63. ['c'-'E']= - 2,
  64. ['d'-'E']= - 1,
  65. ['h'-'E']= 2,
  66. ['k'-'E']= 3,
  67. ['K'-'E']= 3,
  68. ['M'-'E']= 6,
  69. ['G'-'E']= 9,
  70. ['T'-'E']= 12,
  71. ['P'-'E']= 15,
  72. ['E'-'E']= 18,
  73. ['Z'-'E']= 21,
  74. ['Y'-'E']= 24,
  75. };
  76. /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
  77. * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
  78. * function assumes that the unit of numbers is bits not bytes.
  79. */
  80. static double av_strtod(const char *name, char **tail) {
  81. double d;
  82. char *next;
  83. d = strtod(name, &next);
  84. /* if parsing succeeded, check for and interpret postfixes */
  85. if (next!=name) {
  86. if(*next >= 'E' && *next <= 'z'){
  87. int e= si_prefixes[*next - 'E'];
  88. if(e){
  89. if(next[1] == 'i'){
  90. d*= pow( 2, e/0.3);
  91. next+=2;
  92. }else{
  93. d*= pow(10, e);
  94. next++;
  95. }
  96. }
  97. }
  98. if(*next=='B') {
  99. d*=8;
  100. *next++;
  101. }
  102. }
  103. /* if requested, fill in tail with the position after the last parsed
  104. character */
  105. if (tail)
  106. *tail = next;
  107. return d;
  108. }
  109. static int strmatch(const char *s, const char *prefix){
  110. int i;
  111. for(i=0; prefix[i]; i++){
  112. if(prefix[i] != s[i]) return 0;
  113. }
  114. return 1;
  115. }
  116. static double evalPrimary(Parser *p){
  117. double d, d2=NAN;
  118. char *next= p->s;
  119. int i;
  120. /* number */
  121. d= av_strtod(p->s, &next);
  122. if(next != p->s){
  123. p->s= next;
  124. return d;
  125. }
  126. /* named constants */
  127. for(i=0; p->const_name && p->const_name[i]; i++){
  128. if(strmatch(p->s, p->const_name[i])){
  129. p->s+= strlen(p->const_name[i]);
  130. return p->const_value[i];
  131. }
  132. }
  133. p->s= strchr(p->s, '(');
  134. if(p->s==NULL){
  135. *p->error = "missing (";
  136. p->s= next;
  137. return NAN;
  138. }
  139. p->s++; // "("
  140. d= evalExpression(p);
  141. if(p->s[0]== ','){
  142. p->s++; // ","
  143. d2= evalExpression(p);
  144. }
  145. if(p->s[0] != ')'){
  146. *p->error = "missing )";
  147. return NAN;
  148. }
  149. p->s++; // ")"
  150. if( strmatch(next, "sinh" ) ) d= sinh(d);
  151. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  152. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  153. else if( strmatch(next, "sin" ) ) d= sin(d);
  154. else if( strmatch(next, "cos" ) ) d= cos(d);
  155. else if( strmatch(next, "tan" ) ) d= tan(d);
  156. else if( strmatch(next, "atan" ) ) d= atan(d);
  157. else if( strmatch(next, "asin" ) ) d= asin(d);
  158. else if( strmatch(next, "acos" ) ) d= acos(d);
  159. else if( strmatch(next, "exp" ) ) d= exp(d);
  160. else if( strmatch(next, "log" ) ) d= log(d);
  161. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  162. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  163. else if( strmatch(next, "abs" ) ) d= fabs(d);
  164. else if( strmatch(next, "mod" ) ) d-= floor(d/d2)*d2;
  165. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  166. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  167. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  168. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  169. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  170. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  171. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  172. else if( strmatch(next, "(" ) ) d= d;
  173. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  174. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  175. else{
  176. for(i=0; p->func1_name && p->func1_name[i]; i++){
  177. if(strmatch(next, p->func1_name[i])){
  178. return p->func1[i](p->opaque, d);
  179. }
  180. }
  181. for(i=0; p->func2_name && p->func2_name[i]; i++){
  182. if(strmatch(next, p->func2_name[i])){
  183. return p->func2[i](p->opaque, d, d2);
  184. }
  185. }
  186. *p->error = "unknown function";
  187. return NAN;
  188. }
  189. return d;
  190. }
  191. static double evalPow(Parser *p){
  192. int sign= (*p->s == '+') - (*p->s == '-');
  193. p->s += sign&1;
  194. return (sign|1) * evalPrimary(p);
  195. }
  196. static double evalFactor(Parser *p){
  197. double ret= evalPow(p);
  198. while(p->s[0]=='^'){
  199. p->s++;
  200. ret= pow(ret, evalPow(p));
  201. }
  202. return ret;
  203. }
  204. static double evalTerm(Parser *p){
  205. double ret= evalFactor(p);
  206. while(p->s[0]=='*' || p->s[0]=='/'){
  207. if(*p->s++ == '*') ret*= evalFactor(p);
  208. else ret/= evalFactor(p);
  209. }
  210. return ret;
  211. }
  212. static double evalExpression(Parser *p){
  213. double ret= 0;
  214. if(p->stack_index <= 0) //protect against stack overflows
  215. return NAN;
  216. p->stack_index--;
  217. do{
  218. ret += evalTerm(p);
  219. }while(*p->s == '+' || *p->s == '-');
  220. p->stack_index++;
  221. return ret;
  222. }
  223. double ff_eval2(char *s, double *const_value, const char **const_name,
  224. double (**func1)(void *, double), const char **func1_name,
  225. double (**func2)(void *, double, double), char **func2_name,
  226. void *opaque, char **error){
  227. Parser p;
  228. p.stack_index=100;
  229. p.s= s;
  230. p.const_value= const_value;
  231. p.const_name = const_name;
  232. p.func1 = func1;
  233. p.func1_name = func1_name;
  234. p.func2 = func2;
  235. p.func2_name = func2_name;
  236. p.opaque = opaque;
  237. p.error= error;
  238. return evalExpression(&p);
  239. }
  240. #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
  241. attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
  242. double (**func1)(void *, double), const char **func1_name,
  243. double (**func2)(void *, double, double), char **func2_name,
  244. void *opaque){
  245. char *error=NULL;
  246. double ret;
  247. ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
  248. if (error)
  249. av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
  250. return ret;
  251. }
  252. #endif
  253. #ifdef TEST
  254. #undef printf
  255. static double const_values[]={
  256. M_PI,
  257. M_E,
  258. 0
  259. };
  260. static const char *const_names[]={
  261. "PI",
  262. "E",
  263. 0
  264. };
  265. main(){
  266. int i;
  267. 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));
  268. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  269. for(i=0; i<1050; i++){
  270. START_TIMER
  271. 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);
  272. STOP_TIMER("ff_eval")
  273. }
  274. }
  275. #endif