eval.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * Track the presence of variables and their number of occurrences in a parsed expression
  83. *
  84. * @param counter a zero-initialized array where the count of each variable will be stored
  85. * @param size size of array
  86. * @return 0 on success, a negative value indicates that no expression or array was passed
  87. * or size was zero
  88. */
  89. int av_expr_count_vars(AVExpr *e, unsigned *counter, int size);
  90. /**
  91. * Track the presence of user provided functions and their number of occurrences
  92. * in a parsed expression.
  93. *
  94. * @param counter a zero-initialized array where the count of each function will be stored
  95. * if you passed 5 functions with 2 arguments to av_expr_parse()
  96. * then for arg=2 this will use upto 5 entries.
  97. * @param size size of array
  98. * @param arg number of arguments the counted functions have
  99. * @return 0 on success, a negative value indicates that no expression or array was passed
  100. * or size was zero
  101. */
  102. int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg);
  103. /**
  104. * Free a parsed expression previously created with av_expr_parse().
  105. */
  106. void av_expr_free(AVExpr *e);
  107. /**
  108. * Parse the string in numstr and return its value as a double. If
  109. * the string is empty, contains only whitespaces, or does not contain
  110. * an initial substring that has the expected syntax for a
  111. * floating-point number, no conversion is performed. In this case,
  112. * returns a value of zero and the value returned in tail is the value
  113. * of numstr.
  114. *
  115. * @param numstr a string representing a number, may contain one of
  116. * the International System number postfixes, for example 'K', 'M',
  117. * 'G'. If 'i' is appended after the postfix, powers of 2 are used
  118. * instead of powers of 10. The 'B' postfix multiplies the value by
  119. * 8, and can be appended after another postfix or used alone. This
  120. * allows using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  121. * @param tail if non-NULL puts here the pointer to the char next
  122. * after the last parsed character
  123. */
  124. double av_strtod(const char *numstr, char **tail);
  125. #endif /* AVUTIL_EVAL_H */