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