[PATCH 2/3] Change ff_parse_expr() and ff_parse_and_eval_expr() interface.
Stefano Sabatini
stefano.sabatini-lala
Tue Apr 6 00:13:04 CEST 2010
Make them print error messages using a log_ctx rather than set a
constant error string in the Parser. A log_level_offset is used to
change the log_level of the log_ctx, for example to silence eventual
errors issued when evaluating the expression.
Allow the error message to be more expressive, as it is not anymore a
generic const char * string.
---
libavcodec/eval.c | 20 +++++++++++---------
libavcodec/eval.h | 12 ++++++++----
libavcodec/opt.c | 6 ++----
libavcodec/ratecontrol.c | 5 ++---
4 files changed, 23 insertions(+), 20 deletions(-)
diff --git a/libavcodec/eval.c b/libavcodec/eval.c
index ce4d0f5..637f495 100644
--- a/libavcodec/eval.c
+++ b/libavcodec/eval.c
@@ -39,7 +39,8 @@ typedef struct Parser{
double (* const *func2)(void *, double a, double b); // NULL terminated
const char * const *func2_name; // NULL terminated
void *opaque;
- const char **error;
+ void *log_ctx;
+ int log_level_offset;
#define VARS 10
double var[VARS];
} Parser;
@@ -201,7 +202,7 @@ static AVExpr * parse_primary(Parser *p) {
p->s= strchr(p->s, '(');
if(p->s==NULL){
- *p->error = "undefined constant or missing (";
+ av_log(p->log_ctx, AV_LOG_ERROR + p->log_level_offset, "undefined constant or missing (\n");
p->s= next;
ff_free_expr(d);
return NULL;
@@ -211,7 +212,7 @@ static AVExpr * parse_primary(Parser *p) {
av_freep(&d);
d = parse_expr(p);
if(p->s[0] != ')'){
- *p->error = "missing )";
+ av_log(p->log_ctx, AV_LOG_ERROR + p->log_level_offset, "missing )\n");
ff_free_expr(d);
return NULL;
}
@@ -224,7 +225,7 @@ static AVExpr * parse_primary(Parser *p) {
d->param[1] = parse_expr(p);
}
if(p->s[0] != ')'){
- *p->error = "missing )";
+ av_log(p->log_ctx, AV_LOG_ERROR + p->log_level_offset, "missing )\n");
ff_free_expr(d);
return NULL;
}
@@ -273,7 +274,7 @@ static AVExpr * parse_primary(Parser *p) {
}
}
- *p->error = "unknown function";
+ av_log(p->log_ctx, AV_LOG_ERROR + p->log_level_offset, "unknown function\n");
ff_free_expr(d);
return NULL;
}
@@ -372,7 +373,7 @@ static int verify_expr(AVExpr * e) {
AVExpr *ff_parse_expr(const char *s, const char * const *const_name,
double (* const *func1)(void *, double), const char * const *func1_name,
double (* const *func2)(void *, double, double), const char * const *func2_name,
- const char **error){
+ void *log_ctx, int log_level_offset){
Parser p;
AVExpr *e = NULL;
char *w = av_malloc(strlen(s) + 1);
@@ -392,7 +393,8 @@ AVExpr *ff_parse_expr(const char *s, const char * const *const_name,
p.func1_name = func1_name;
p.func2 = func2;
p.func2_name = func2_name;
- p.error= error;
+ p.log_ctx = log_ctx;
+ p.log_level_offset = log_level_offset;
e = parse_expr(&p);
if (!verify_expr(e)) {
@@ -415,8 +417,8 @@ double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) {
double ff_parse_and_eval_expr(const char *s, const double *const_value, const char * const *const_name,
double (* const *func1)(void *, double), const char * const *func1_name,
double (* const *func2)(void *, double, double), const char * const *func2_name,
- void *opaque, const char **error){
- AVExpr * e = ff_parse_expr(s, const_name, func1, func1_name, func2, func2_name, error);
+ void *opaque, void *log_ctx, int log_level_offset){
+ AVExpr * e = ff_parse_expr(s, const_name, func1, func1_name, func2, func2_name, log_ctx, log_level_offset);
double d;
if (!e) return NAN;
d = ff_eval_expr(e, const_value, opaque);
diff --git a/libavcodec/eval.h b/libavcodec/eval.h
index 50c16af..4effe76 100644
--- a/libavcodec/eval.h
+++ b/libavcodec/eval.h
@@ -38,15 +38,17 @@ typedef struct AVExpr AVExpr;
* @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
* @param func2_name NULL terminated array of zero terminated strings of func2 identifers
- * @param error pointer to a char* which is set to an error message if something goes wrong
* @param const_value a zero terminated array of values for the identifers from const_name
* @param opaque a pointer which will be passed to all functions from func1 and func2
+ * @param log_ctx logging context
+ * @param log_level_offset log level offset applied to the global log
+ * level when logging with log_ctx
* @return the value of the expression
*/
double ff_parse_and_eval_expr(const char *s, const double *const_value, const char * const *const_name,
double (* const *func1)(void *, double), const char * const *func1_name,
double (* const *func2)(void *, double, double), const char * const *func2_name,
- void *opaque, const char **error);
+ void *opaque, void *log_ctx, int log_level_offset);
/**
* Parses an expression.
@@ -57,14 +59,16 @@ double ff_parse_and_eval_expr(const char *s, const double *const_value, const ch
* @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
* @param func2_name NULL terminated array of zero terminated strings of func2 identifers
- * @param error pointer to a char* which is set to an error message if something goes wrong
+ * @param log_ctx logging context
+ * @param log_level_offset log level offset applied to the global log
+ * level when logging with log_ctx
* @return AVExpr which must be freed with ff_free_expr() by the user when it is not needed anymore
* NULL if anything went wrong
*/
AVExpr *ff_parse_expr(const char *s, const char * const *const_name,
double (* const *func1)(void *, double), const char * const *func1_name,
double (* const *func2)(void *, double, double), const char * const *func2_name,
- const char **error);
+ void *log_ctx, int log_level_offset);
/**
* Evaluates a previously parsed expression.
diff --git a/libavcodec/opt.c b/libavcodec/opt.c
index f9cba05..68546d7 100644
--- a/libavcodec/opt.c
+++ b/libavcodec/opt.c
@@ -147,7 +147,6 @@ int av_set_string3(void *obj, const char *name, const char *val, int alloc, cons
char buf[256];
int cmd=0;
double d;
- const char *error = NULL;
if(*val == '+' || *val == '-')
cmd= *(val++);
@@ -156,7 +155,7 @@ int av_set_string3(void *obj, const char *name, const char *val, int alloc, cons
buf[i]= val[i];
buf[i]=0;
- d = ff_parse_and_eval_expr(buf, const_values, const_names, NULL, NULL, NULL, NULL, NULL, &error);
+ d = ff_parse_and_eval_expr(buf, const_values, const_names, NULL, NULL, NULL, NULL, NULL, obj, AV_LOG_DEBUG - AV_LOG_QUIET);
if(isnan(d)) {
const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
if(o_named && o_named->type == FF_OPT_TYPE_CONST)
@@ -167,8 +166,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 {
- if (error)
- av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error);
+ av_log(obj, AV_LOG_ERROR, "Unable to parse or evaluate option value \"%s\"\n", val);
return AVERROR(EINVAL);
}
}
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index e52ef1a..5d3193c 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -67,7 +67,6 @@ int ff_rate_control_init(MpegEncContext *s)
{
RateControlContext *rcc= &s->rc_context;
int i;
- const char *error = NULL;
static const char * const const_names[]={
"PI",
"E",
@@ -107,9 +106,9 @@ 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, func1_names, NULL, NULL, &error);
+ rcc->rc_eq_eval = ff_parse_expr(s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1, func1_names, NULL, NULL, s->avctx, AV_LOG_DEBUG - AV_LOG_QUIET);
if (!rcc->rc_eq_eval) {
- av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\": %s\n", s->avctx->rc_eq, error? error : "");
+ av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq);
return -1;
}
--
1.7.0
--3MwIy2ne0vdjdPXF--
More information about the ffmpeg-devel
mailing list