Commit 9ace13b4 authored by Stefano Sabatini's avatar Stefano Sabatini

Make ff_parse_expr() and ff_parse_and_eval_expr() return an int

containing an error code.

Allow these functions to convey the reason of the failure to the
calling function, failure which is not always due to a parsing error
but it may depend for example on a memory problem.

Also fix several potential memleaks.

Originally committed as revision 23402 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 27241cbf
......@@ -31,7 +31,7 @@
#define LIBAVCODEC_VERSION_MAJOR 52
#define LIBAVCODEC_VERSION_MINOR 72
#define LIBAVCODEC_VERSION_MICRO 0
#define LIBAVCODEC_VERSION_MICRO 1
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
......
This diff is collapsed.
......@@ -32,6 +32,8 @@ typedef struct AVExpr AVExpr;
* Parses and evaluates an expression.
* Note, this is significantly slower than ff_eval_expr().
*
* @param res a pointer to a double where is put the result value of
* the expression, or NAN in case of error
* @param s expression as a zero terminated string for example "1+2^3+5*5+sin(2/3)"
* @param const_name NULL terminated array of zero terminated strings of constant identifers for example {"PI", "E", 0}
* @param const_value a zero terminated array of values for the identifers from const_name
......@@ -41,9 +43,10 @@ typedef struct AVExpr AVExpr;
* @param func2 NULL terminated array of function pointers for functions which take 2 arguments
* @param opaque a pointer which will be passed to all functions from func1 and func2
* @param log_ctx parent logging context
* @return the value of the expression
* @return 0 in case of success, a negative value corresponding to an
* AVERROR code otherwise
*/
double ff_parse_and_eval_expr(const char *s,
int ff_parse_and_eval_expr(double *res, const char *s,
const char * const *const_name, const double *const_value,
const char * const *func1_name, double (* const *func1)(void *, double),
const char * const *func2_name, double (* const *func2)(void *, double, double),
......@@ -52,6 +55,10 @@ double ff_parse_and_eval_expr(const char *s,
/**
* Parses an expression.
*
* @param expr a pointer where is put an AVExpr containing the parsed
* value in case of successfull parsing, or NULL otherwise.
* The pointed to AVExpr must be freed with ff_free_expr() by the user
* when it is not needed anymore.
* @param s expression as a zero terminated string for example "1+2^3+5*5+sin(2/3)"
* @param const_name NULL terminated array of zero terminated strings of constant identifers for example {"PI", "E", 0}
* @param func1_name NULL terminated array of zero terminated strings of func1 identifers
......@@ -59,10 +66,10 @@ double ff_parse_and_eval_expr(const char *s,
* @param func2_name NULL terminated array of zero terminated strings of func2 identifers
* @param func2 NULL terminated array of function pointers for functions which take 2 arguments
* @param log_ctx parent logging context
* @return AVExpr which must be freed with ff_free_expr() by the user when it is not needed anymore
* NULL if anything went wrong
* @return 0 in case of success, a negative value corresponding to an
* AVERROR code otherwise
*/
AVExpr *ff_parse_expr(const char *s,
int ff_parse_expr(AVExpr **expr, const char *s,
const char * const *const_name,
const char * const *func1_name, double (* const *func1)(void *, double),
const char * const *func2_name, double (* const *func2)(void *, double, double),
......
......@@ -165,10 +165,10 @@ int av_set_string3(void *obj, const char *name, const char *val, int alloc, cons
else if(!strcmp(buf, "none" )) d= 0;
else if(!strcmp(buf, "all" )) d= ~0;
else {
d = ff_parse_and_eval_expr(buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
if (isnan(d)){
int res = ff_parse_and_eval_expr(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
if (res < 0) {
av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
return AVERROR(EINVAL);
return res;
}
}
}
......
......@@ -66,7 +66,7 @@ static inline double bits2qp(RateControlEntry *rce, double bits){
int ff_rate_control_init(MpegEncContext *s)
{
RateControlContext *rcc= &s->rc_context;
int i;
int i, res;
static const char * const const_names[]={
"PI",
"E",
......@@ -106,10 +106,10 @@ int ff_rate_control_init(MpegEncContext *s)
};
emms_c();
rcc->rc_eq_eval = ff_parse_expr(s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx);
if (!rcc->rc_eq_eval) {
res = ff_parse_expr(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx);
if (res < 0) {
av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq);
return -1;
return res;
}
for(i=0; i<5; i++){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment