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.

141 lines
6.4KB

  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. typedef struct AVExpr AVExpr;
  27. /**
  28. * Parse and evaluate an expression.
  29. * Note, this is significantly slower than av_expr_eval().
  30. *
  31. * @param res a pointer to a double where is put the result value of
  32. * the expression, or NAN in case of error
  33. * @param s expression as a zero terminated string, for example "1+2^3+5*5+sin(2/3)"
  34. * @param const_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
  35. * @param const_values a zero terminated array of values for the identifiers from const_names
  36. * @param func1_names NULL terminated array of zero terminated strings of funcs1 identifiers
  37. * @param funcs1 NULL terminated array of function pointers for functions which take 1 argument
  38. * @param func2_names NULL terminated array of zero terminated strings of funcs2 identifiers
  39. * @param funcs2 NULL terminated array of function pointers for functions which take 2 arguments
  40. * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
  41. * @param log_offset log level offset, can be used to silence error messages
  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_offset log level offset, can be used to silence error messages
  65. * @param log_ctx parent logging context
  66. * @return >= 0 in case of success, a negative value corresponding to an
  67. * AVERROR code otherwise
  68. */
  69. int av_expr_parse(AVExpr **expr, const char *s,
  70. const char * const *const_names,
  71. const char * const *func1_names, double (* const *funcs1)(void *, double),
  72. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  73. int log_offset, void *log_ctx);
  74. /**
  75. * Evaluate a previously parsed expression.
  76. *
  77. * @param e the AVExpr to evaluate
  78. * @param const_values a zero terminated array of values for the identifiers from av_expr_parse() const_names
  79. * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
  80. * @return the value of the expression
  81. */
  82. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque);
  83. /**
  84. * Track the presence of variables and their number of occurrences in a parsed expression
  85. *
  86. * @param e the AVExpr to track variables in
  87. * @param counter a zero-initialized array where the count of each variable will be stored
  88. * @param size size of array
  89. * @return 0 on success, a negative value indicates that no expression or array was passed
  90. * or size was zero
  91. */
  92. int av_expr_count_vars(AVExpr *e, unsigned *counter, int size);
  93. /**
  94. * Track the presence of user provided functions and their number of occurrences
  95. * in a parsed expression.
  96. *
  97. * @param e the AVExpr to track user provided functions in
  98. * @param counter a zero-initialized array where the count of each function will be stored
  99. * if you passed 5 functions with 2 arguments to av_expr_parse()
  100. * then for arg=2 this will use upto 5 entries.
  101. * @param size size of array
  102. * @param arg number of arguments the counted functions have
  103. * @return 0 on success, a negative value indicates that no expression or array was passed
  104. * or size was zero
  105. */
  106. int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg);
  107. /**
  108. * Free a parsed expression previously created with av_expr_parse().
  109. */
  110. void av_expr_free(AVExpr *e);
  111. /**
  112. * Parse the string in numstr and return its value as a double. If
  113. * the string is empty, contains only whitespaces, or does not contain
  114. * an initial substring that has the expected syntax for a
  115. * floating-point number, no conversion is performed. In this case,
  116. * returns a value of zero and the value returned in tail is the value
  117. * of numstr.
  118. *
  119. * @param numstr a string representing a number, may contain one of
  120. * the International System number postfixes, for example 'K', 'M',
  121. * 'G'. If 'i' is appended after the postfix, powers of 2 are used
  122. * instead of powers of 10. The 'B' postfix multiplies the value by
  123. * 8, and can be appended after another postfix or used alone. This
  124. * allows using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  125. * @param tail if non-NULL puts here the pointer to the char next
  126. * after the last parsed character
  127. */
  128. double av_strtod(const char *numstr, char **tail);
  129. #endif /* AVUTIL_EVAL_H */