eval.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * simple arithmetic expression evaluator
  23. */
  24. #ifndef AVUTIL_EVAL_H
  25. #define AVUTIL_EVAL_H
  26. #include "avutil.h"
  27. typedef struct AVExpr AVExpr;
  28. /**
  29. * Parse and evaluate an expression.
  30. * Note, this is significantly slower than av_expr_eval().
  31. *
  32. * @param res a pointer to a double where is put the result value of
  33. * the expression, or NAN in case of error
  34. * @param s expression as a zero terminated string, for example "1+2^3+5*5+sin(2/3)"
  35. * @param const_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
  36. * @param const_values a zero terminated array of values for the identifiers from const_names
  37. * @param func1_names NULL terminated array of zero terminated strings of funcs1 identifiers
  38. * @param funcs1 NULL terminated array of function pointers for functions which take 1 argument
  39. * @param func2_names NULL terminated array of zero terminated strings of funcs2 identifiers
  40. * @param funcs2 NULL terminated array of function pointers for functions which take 2 arguments
  41. * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
  42. * @param log_ctx parent logging context
  43. * @return 0 in case of success, a negative value corresponding to an
  44. * AVERROR code otherwise
  45. */
  46. int av_expr_parse_and_eval(double *res, const char *s,
  47. const char * const *const_names, const double *const_values,
  48. const char * const *func1_names, double (* const *funcs1)(void *, double),
  49. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  50. void *opaque, int log_offset, void *log_ctx);
  51. /**
  52. * Parse an expression.
  53. *
  54. * @param expr a pointer where is put an AVExpr containing the parsed
  55. * value in case of successful parsing, or NULL otherwise.
  56. * The pointed to AVExpr must be freed with av_expr_free() by the user
  57. * when it is not needed anymore.
  58. * @param s expression as a zero terminated string, for example "1+2^3+5*5+sin(2/3)"
  59. * @param const_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
  60. * @param func1_names NULL terminated array of zero terminated strings of funcs1 identifiers
  61. * @param funcs1 NULL terminated array of function pointers for functions which take 1 argument
  62. * @param func2_names NULL terminated array of zero terminated strings of funcs2 identifiers
  63. * @param funcs2 NULL terminated array of function pointers for functions which take 2 arguments
  64. * @param log_ctx parent logging context
  65. * @return 0 in case of success, a negative value corresponding to an
  66. * AVERROR code otherwise
  67. */
  68. int av_expr_parse(AVExpr **expr, const char *s,
  69. const char * const *const_names,
  70. const char * const *func1_names, double (* const *funcs1)(void *, double),
  71. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  72. int log_offset, void *log_ctx);
  73. /**
  74. * Evaluate a previously parsed expression.
  75. *
  76. * @param const_values a zero terminated array of values for the identifiers from av_expr_parse() const_names
  77. * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
  78. * @return the value of the expression
  79. */
  80. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque);
  81. /**
  82. * Free a parsed expression previously created with av_expr_parse().
  83. */
  84. void av_expr_free(AVExpr *e);
  85. #if FF_API_OLD_EVAL_NAMES
  86. /**
  87. * @deprecated Deprecated in favor of av_expr_parse_and_eval().
  88. */
  89. attribute_deprecated
  90. int av_parse_and_eval_expr(double *res, const char *s,
  91. const char * const *const_names, const double *const_values,
  92. const char * const *func1_names, double (* const *funcs1)(void *, double),
  93. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  94. void *opaque, int log_offset, void *log_ctx);
  95. /**
  96. * @deprecated Deprecated in favor of av_expr_parse().
  97. */
  98. attribute_deprecated
  99. int av_parse_expr(AVExpr **expr, const char *s,
  100. const char * const *const_names,
  101. const char * const *func1_names, double (* const *funcs1)(void *, double),
  102. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  103. int log_offset, void *log_ctx);
  104. /**
  105. * @deprecated Deprecated in favor of av_expr_eval().
  106. */
  107. attribute_deprecated
  108. double av_eval_expr(AVExpr *e, const double *const_values, void *opaque);
  109. /**
  110. * @deprecated Deprecated in favor of av_expr_free().
  111. */
  112. attribute_deprecated
  113. void av_free_expr(AVExpr *e);
  114. #endif /* FF_API_OLD_EVAL_NAMES */
  115. /**
  116. * Parse the string in numstr and return its value as a double. If
  117. * the string is empty, contains only whitespaces, or does not contain
  118. * an initial substring that has the expected syntax for a
  119. * floating-point number, no conversion is performed. In this case,
  120. * returns a value of zero and the value returned in tail is the value
  121. * of numstr.
  122. *
  123. * @param numstr a string representing a number, may contain one of
  124. * the International System number postfixes, for example 'K', 'M',
  125. * 'G'. If 'i' is appended after the postfix, powers of 2 are used
  126. * instead of powers of 10. The 'B' postfix multiplies the value for
  127. * 8, and can be appended after another postfix or used alone. This
  128. * allows using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  129. * @param tail if non-NULL puts here the pointer to the char next
  130. * after the last parsed character
  131. */
  132. double av_strtod(const char *numstr, char **tail);
  133. #endif /* AVUTIL_EVAL_H */