Use hierarchic names convention (prefix them with av_expr) for the

eval API.

More grep-friendly and more consistent with the rest of the FFmpeg
API.

Originally committed as revision 25708 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Stefano Sabatini 2010-11-08 14:06:49 +00:00
parent 24de0edbd5
commit d2af7205a1
11 changed files with 147 additions and 60 deletions

View File

@ -13,6 +13,18 @@ libavutil: 2009-03-08
API changes, most recent first:
2010-11-08 - r25708 - lavu 50.33.0 - eval.h
Deprecate functions:
av_parse_and_eval_expr(),
av_parse_expr(),
av_eval_expr(),
av_free_expr(),
in favor of the functions:
av_expr_parse_and_eval(),
av_expr_parse(),
av_expr_eval(),
av_expr_free().
2010-11-08 - r25707 - lavfi 1.59.0 - avfilter_free()
Rename avfilter_destroy() to avfilter_free().
This change breaks libavfilter API/ABI.

View File

@ -106,7 +106,7 @@ int ff_rate_control_init(MpegEncContext *s)
};
emms_c();
res = av_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);
res = av_expr_parse(&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 res;
@ -254,7 +254,7 @@ void ff_rate_control_uninit(MpegEncContext *s)
RateControlContext *rcc= &s->rc_context;
emms_c();
av_free_expr(rcc->rc_eq_eval);
av_expr_free(rcc->rc_eq_eval);
av_freep(&rcc->entry);
#if CONFIG_LIBXVID
@ -338,7 +338,7 @@ static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_f
0
};
bits = av_eval_expr(rcc->rc_eq_eval, const_values, rce);
bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
if (isnan(bits)) {
av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", s->avctx->rc_eq);
return -1;

View File

@ -128,7 +128,7 @@ int av_parse_video_rate(AVRational *rate, const char *arg)
}
/* Then, we try to parse it as fraction */
if ((ret = av_parse_and_eval_expr(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL,
if ((ret = av_expr_parse_and_eval(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, 0, NULL)) < 0)
return ret;
*rate = av_d2q(res, 1001000);

View File

@ -125,8 +125,8 @@ static av_cold void uninit(AVFilterContext *ctx)
{
CropContext *crop = ctx->priv;
av_free_expr(crop->x_pexpr); crop->x_pexpr = NULL;
av_free_expr(crop->y_pexpr); crop->y_pexpr = NULL;
av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
}
static inline int normalize_double(int *n, double d)
@ -170,16 +170,16 @@ static int config_input(AVFilterLink *link)
crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
if ((ret = av_parse_and_eval_expr(&res, (expr = crop->ow_expr),
if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
var_names, crop->var_values,
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
if ((ret = av_parse_and_eval_expr(&res, (expr = crop->oh_expr),
if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
var_names, crop->var_values,
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
/* evaluate again ow as it may depend on oh */
if ((ret = av_parse_and_eval_expr(&res, (expr = crop->ow_expr),
if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
var_names, crop->var_values,
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
@ -194,9 +194,9 @@ static int config_input(AVFilterLink *link)
crop->w &= ~((1 << crop->hsub) - 1);
crop->h &= ~((1 << crop->vsub) - 1);
if ((ret = av_parse_expr(&crop->x_pexpr, crop->x_expr, var_names,
if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
(ret = av_parse_expr(&crop->y_pexpr, crop->y_expr, var_names,
(ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
NULL, NULL, NULL, NULL, 0, ctx)) < 0)
return AVERROR(EINVAL);
@ -243,9 +243,9 @@ static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
crop->var_values[VAR_T] = picref->pts == AV_NOPTS_VALUE ?
NAN : picref->pts * av_q2d(link->time_base);
crop->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
crop->var_values[VAR_X] = av_eval_expr(crop->x_pexpr, crop->var_values, NULL);
crop->var_values[VAR_Y] = av_eval_expr(crop->y_pexpr, crop->var_values, NULL);
crop->var_values[VAR_X] = av_eval_expr(crop->x_pexpr, crop->var_values, NULL);
crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
normalize_double(&crop->x, crop->var_values[VAR_X]);
normalize_double(&crop->y, crop->var_values[VAR_Y]);

View File

@ -69,7 +69,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
SetPTSContext *setpts = ctx->priv;
int ret;
if ((ret = av_parse_expr(&setpts->expr, args ? args : "PTS",
if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
return ret;
@ -111,7 +111,7 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
d = av_eval_expr(setpts->expr, setpts->var_values, NULL);
d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
outpicref->pts = D2TS(d);
#ifdef DEBUG
@ -133,7 +133,7 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
static av_cold void uninit(AVFilterContext *ctx)
{
SetPTSContext *setpts = ctx->priv;
av_free_expr(setpts->expr);
av_expr_free(setpts->expr);
setpts->expr = NULL;
}

View File

@ -81,7 +81,7 @@ static int config_output_props(AVFilterLink *outlink)
outlink->w = inlink->w;
outlink->h = inlink->h;
if ((ret = av_parse_and_eval_expr(&res, settb->tb_expr, var_names, settb->var_values,
if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
return ret;

View File

@ -80,7 +80,7 @@ static int config_props(AVFilterLink *outlink)
priv->var_values[VAR_PI] = M_PI;
priv->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
if ((ret = av_parse_and_eval_expr(&res, priv->tb_expr, var_names, priv->var_values,
if ((ret = av_expr_parse_and_eval(&res, priv->tb_expr, var_names, priv->var_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", priv->tb_expr);
return ret;

View File

@ -40,8 +40,8 @@
#define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
#define LIBAVUTIL_VERSION_MAJOR 50
#define LIBAVUTIL_VERSION_MINOR 32
#define LIBAVUTIL_VERSION_MICRO 6
#define LIBAVUTIL_VERSION_MINOR 33
#define LIBAVUTIL_VERSION_MICRO 0
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \
@ -53,6 +53,14 @@
#define LIBAVUTIL_IDENT "Lavu" AV_STRINGIFY(LIBAVUTIL_VERSION)
/**
* Those FF_API_* defines are not part of public API.
* They may change, break or disappear at any time.
*/
#ifndef FF_API_OLD_EVAL_NAMES
#define FF_API_OLD_EVAL_NAMES (LIBAVUTIL_VERSION_MAJOR < 51)
#endif
/**
* Return the LIBAVUTIL_VERSION_INT constant.
*/

View File

@ -175,11 +175,11 @@ static double eval_expr(Parser *p, AVExpr *e)
static int parse_expr(AVExpr **e, Parser *p);
void av_free_expr(AVExpr *e)
void av_expr_free(AVExpr *e)
{
if (!e) return;
av_free_expr(e->param[0]);
av_free_expr(e->param[1]);
av_expr_free(e->param[0]);
av_expr_free(e->param[1]);
av_freep(&e);
}
@ -217,7 +217,7 @@ static int parse_primary(AVExpr **e, Parser *p)
if (p->s==NULL) {
av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
p->s= next;
av_free_expr(d);
av_expr_free(d);
return AVERROR(EINVAL);
}
p->s++; // "("
@ -227,7 +227,7 @@ static int parse_primary(AVExpr **e, Parser *p)
return ret;
if (p->s[0] != ')') {
av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
av_free_expr(d);
av_expr_free(d);
return AVERROR(EINVAL);
}
p->s++; // ")"
@ -235,7 +235,7 @@ static int parse_primary(AVExpr **e, Parser *p)
return 0;
}
if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
av_free_expr(d);
av_expr_free(d);
return ret;
}
if (p->s[0]== ',') {
@ -244,7 +244,7 @@ static int parse_primary(AVExpr **e, Parser *p)
}
if (p->s[0] != ')') {
av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
av_free_expr(d);
av_expr_free(d);
return AVERROR(EINVAL);
}
p->s++; // ")"
@ -296,7 +296,7 @@ static int parse_primary(AVExpr **e, Parser *p)
}
av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
av_free_expr(d);
av_expr_free(d);
return AVERROR(EINVAL);
}
@ -333,13 +333,13 @@ static int parse_factor(AVExpr **e, Parser *p)
e1 = e0;
p->s++;
if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
av_free_expr(e1);
av_expr_free(e1);
return ret;
}
e0 = new_eval_expr(e_pow, 1, e1, e2);
if (!e0) {
av_free_expr(e1);
av_free_expr(e2);
av_expr_free(e1);
av_expr_free(e2);
return AVERROR(ENOMEM);
}
if (e0->param[1]) e0->param[1]->value *= (sign2|1);
@ -360,13 +360,13 @@ static int parse_term(AVExpr **e, Parser *p)
int c= *p->s++;
e1 = e0;
if ((ret = parse_factor(&e2, p)) < 0) {
av_free_expr(e1);
av_expr_free(e1);
return ret;
}
e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
if (!e0) {
av_free_expr(e1);
av_free_expr(e2);
av_expr_free(e1);
av_expr_free(e2);
return AVERROR(ENOMEM);
}
}
@ -383,13 +383,13 @@ static int parse_subexpr(AVExpr **e, Parser *p)
while (*p->s == '+' || *p->s == '-') {
e1 = e0;
if ((ret = parse_term(&e2, p)) < 0) {
av_free_expr(e1);
av_expr_free(e1);
return ret;
}
e0 = new_eval_expr(e_add, 1, e1, e2);
if (!e0) {
av_free_expr(e1);
av_free_expr(e2);
av_expr_free(e1);
av_expr_free(e2);
return AVERROR(ENOMEM);
}
};
@ -412,13 +412,13 @@ static int parse_expr(AVExpr **e, Parser *p)
p->s++;
e1 = e0;
if ((ret = parse_subexpr(&e2, p)) < 0) {
av_free_expr(e1);
av_expr_free(e1);
return ret;
}
e0 = new_eval_expr(e_last, 1, e1, e2);
if (!e0) {
av_free_expr(e1);
av_free_expr(e2);
av_expr_free(e1);
av_expr_free(e2);
return AVERROR(ENOMEM);
}
};
@ -444,7 +444,7 @@ static int verify_expr(AVExpr *e)
}
}
int av_parse_expr(AVExpr **expr, const char *s,
int av_expr_parse(AVExpr **expr, const char *s,
const char * const *const_names,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
@ -483,7 +483,7 @@ int av_parse_expr(AVExpr **expr, const char *s,
goto end;
}
if (!verify_expr(e)) {
av_free_expr(e);
av_expr_free(e);
ret = AVERROR(EINVAL);
goto end;
}
@ -493,7 +493,7 @@ end:
return ret;
}
double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
{
Parser p;
@ -502,24 +502,56 @@ double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
return eval_expr(&p, e);
}
int av_parse_and_eval_expr(double *d, const char *s,
int av_expr_parse_and_eval(double *d, const char *s,
const char * const *const_names, const double *const_values,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
void *opaque, int log_offset, void *log_ctx)
{
AVExpr *e = NULL;
int ret = av_parse_expr(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
if (ret < 0) {
*d = NAN;
return ret;
}
*d = av_eval_expr(e, const_values, opaque);
av_free_expr(e);
*d = av_expr_eval(e, const_values, opaque);
av_expr_free(e);
return isnan(*d) ? AVERROR(EINVAL) : 0;
}
#if FF_API_OLD_EVAL_NAMES
int av_parse_expr(AVExpr **expr, const char *s,
const char * const *const_names,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
int log_offset, void *log_ctx)
{
return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
log_offset, log_ctx);
}
double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
{
return av_expr_eval(e, const_values, opaque);
}
int av_parse_and_eval_expr(double *res, const char *s,
const char * const *const_names, const double *const_values,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
void *opaque, int log_offset, void *log_ctx)
{
return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
opaque, log_offset, log_ctx);
}
void av_free_expr(AVExpr *e)
{
av_expr_free(e);
}
#endif /* FF_API_OLD_EVAL_NAMES */
#ifdef TEST
#undef printf
static double const_values[] = {
@ -584,27 +616,27 @@ int main(void)
for (expr = exprs; *expr; expr++) {
printf("Evaluating '%s'\n", *expr);
av_parse_and_eval_expr(&d, *expr,
av_expr_parse_and_eval(&d, *expr,
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL);
printf("'%s' -> %f\n\n", *expr, d);
}
av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL);
printf("%f == 12.7\n", d);
av_parse_and_eval_expr(&d, "80G/80Gi",
av_expr_parse_and_eval(&d, "80G/80Gi",
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL);
printf("%f == 0.931322575\n", d);
for (i=0; i<1050; i++) {
START_TIMER
av_parse_and_eval_expr(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL);
STOP_TIMER("av_parse_and_eval_expr")
STOP_TIMER("av_expr_parse_and_eval")
}
return 0;
}

View File

@ -26,11 +26,13 @@
#ifndef AVUTIL_EVAL_H
#define AVUTIL_EVAL_H
#include "avutil.h"
typedef struct AVExpr AVExpr;
/**
* Parse and evaluate an expression.
* Note, this is significantly slower than av_eval_expr().
* Note, this is significantly slower than av_expr_eval().
*
* @param res a pointer to a double where is put the result value of
* the expression, or NAN in case of error
@ -46,7 +48,7 @@ typedef struct AVExpr AVExpr;
* @return 0 in case of success, a negative value corresponding to an
* AVERROR code otherwise
*/
int av_parse_and_eval_expr(double *res, const char *s,
int av_expr_parse_and_eval(double *res, const char *s,
const char * const *const_names, const double *const_values,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
@ -57,7 +59,7 @@ int av_parse_and_eval_expr(double *res, const char *s,
*
* @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 av_free_expr() by the user
* The pointed to AVExpr must be freed with av_expr_free() 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_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
@ -69,7 +71,7 @@ int av_parse_and_eval_expr(double *res, const char *s,
* @return 0 in case of success, a negative value corresponding to an
* AVERROR code otherwise
*/
int av_parse_expr(AVExpr **expr, const char *s,
int av_expr_parse(AVExpr **expr, const char *s,
const char * const *const_names,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
@ -78,16 +80,49 @@ int av_parse_expr(AVExpr **expr, const char *s,
/**
* Evaluate a previously parsed expression.
*
* @param const_values a zero terminated array of values for the identifiers from av_parse_expr() const_names
* @param const_values a zero terminated array of values for the identifiers from av_expr_parse() const_names
* @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
* @return the value of the expression
*/
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque);
/**
* Free a parsed expression previously created with av_expr_parse().
*/
void av_expr_free(AVExpr *e);
#if FF_API_OLD_EVAL_NAMES
/**
* @deprecated Deprecated in favor of av_expr_parse_and_eval().
*/
attribute_deprecated
int av_parse_and_eval_expr(double *res, const char *s,
const char * const *const_names, const double *const_values,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
void *opaque, int log_offset, void *log_ctx);
/**
* @deprecated Deprecated in favor of av_expr_parse().
*/
attribute_deprecated
int av_parse_expr(AVExpr **expr, const char *s,
const char * const *const_names,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
int log_offset, void *log_ctx);
/**
* @deprecated Deprecated in favor of av_expr_eval().
*/
attribute_deprecated
double av_eval_expr(AVExpr *e, const double *const_values, void *opaque);
/**
* Free a parsed expression previously created with av_parse_expr().
* @deprecated Deprecated in favor of av_expr_free().
*/
attribute_deprecated
void av_free_expr(AVExpr *e);
#endif /* FF_API_OLD_EVAL_NAMES */
/**
* Parse the string in numstr and return its value as a double. If

View File

@ -171,7 +171,7 @@ 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 {
int res = av_parse_and_eval_expr(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
int res = av_expr_parse_and_eval(&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 res;