eval.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_ctx parent logging context
  42. * @return >= 0 in case of success, a negative value corresponding to an
  43. * AVERROR code otherwise
  44. */
  45. int av_expr_parse_and_eval(double *res, const char *s,
  46. const char * const *const_names, const double *const_values,
  47. const char * const *func1_names, double (* const *funcs1)(void *, double),
  48. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  49. void *opaque, int log_offset, void *log_ctx);
  50. /**
  51. * Parse an expression.
  52. *
  53. * @param expr a pointer where is put an AVExpr containing the parsed
  54. * value in case of successful parsing, or NULL otherwise.
  55. * The pointed to AVExpr must be freed with av_expr_free() by the user
  56. * when it is not needed anymore.
  57. * @param s expression as a zero terminated string, for example "1+2^3+5*5+sin(2/3)"
  58. * @param const_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
  59. * @param func1_names NULL terminated array of zero terminated strings of funcs1 identifiers
  60. * @param funcs1 NULL terminated array of function pointers for functions which take 1 argument
  61. * @param func2_names NULL terminated array of zero terminated strings of funcs2 identifiers
  62. * @param funcs2 NULL terminated array of function pointers for functions which take 2 arguments
  63. * @param log_ctx parent logging context
  64. * @return >= 0 in case of success, a negative value corresponding to an
  65. * AVERROR code otherwise
  66. */
  67. int av_expr_parse(AVExpr **expr, const char *s,
  68. const char * const *const_names,
  69. const char * const *func1_names, double (* const *funcs1)(void *, double),
  70. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  71. int log_offset, void *log_ctx);
  72. /**
  73. * Evaluate a previously parsed expression.
  74. *
  75. * @param const_values a zero terminated array of values for the identifiers from av_expr_parse() const_names
  76. * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
  77. * @return the value of the expression
  78. */
  79. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque);
  80. /**
  81. * Track the presence of variables and their number of occurrences in a parsed expression
  82. *
  83. * @param counter a zero-initialized array where the count of each variable will be stored
  84. * @param size size of array
  85. * @return 0 on success, a negative value indicates that no expression or array was passed
  86. * or size was zero
  87. */
  88. int av_expr_count_vars(AVExpr *e, unsigned *counter, int size);
  89. /**
  90. * Track the presence of user provided functions and their number of occurrences
  91. * in a parsed expression.
  92. *
  93. * @param counter a zero-initialized array where the count of each function will be stored
  94. * if you passed 5 functions with 2 arguments to av_expr_parse()
  95. * then for arg=2 this will use upto 5 entries.
  96. * @param size size of array
  97. * @param arg number of arguments the counted functions have
  98. * @return 0 on success, a negative value indicates that no expression or array was passed
  99. * or size was zero
  100. */
  101. int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg);
  102. /**
  103. * Free a parsed expression previously created with av_expr_parse().
  104. */
  105. void av_expr_free(AVExpr *e);
  106. /**
  107. * Parse the string in numstr and return its value as a double. If
  108. * the string is empty, contains only whitespaces, or does not contain
  109. * an initial substring that has the expected syntax for a
  110. * floating-point number, no conversion is performed. In this case,
  111. * returns a value of zero and the value returned in tail is the value
  112. * of numstr.
  113. *
  114. * @param numstr a string representing a number, may contain one of
  115. * the International System number postfixes, for example 'K', 'M',
  116. * 'G'. If 'i' is appended after the postfix, powers of 2 are used
  117. * instead of powers of 10. The 'B' postfix multiplies the value by
  118. * 8, and can be appended after another postfix or used alone. This
  119. * allows using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  120. * @param tail if non-NULL puts here the pointer to the char next
  121. * after the last parsed character
  122. */
  123. double av_strtod(const char *numstr, char **tail);
  124. #endif /* AVUTIL_EVAL_H */