[PATCH 3/5] 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.
Allow the error message to be more expressive, as it is not anymore a
generic const char * string, and reference the provided input.
---
libavcodec/eval.c | 20 +++++++++++---------
libavcodec/eval.h | 8 ++++----
libavcodec/opt.c | 8 ++++----
libavcodec/ratecontrol.c | 6 +++---
4 files changed, 22 insertions(+), 20 deletions(-)
diff --git a/libavcodec/eval.c b/libavcodec/eval.c
index 0eadf1c..8d0b8b4 100644
--- a/libavcodec/eval.c
+++ b/libavcodec/eval.c
@@ -39,7 +39,7 @@ 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;
#define VARS 10
double var[VARS];
} Parser;
@@ -201,7 +201,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, "undefined constant or missing (\n");
p->s= next;
ff_free_expr(d);
return NULL;
@@ -211,7 +211,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, "missing )\n");
ff_free_expr(d);
return NULL;
}
@@ -224,7 +224,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, "missing )\n");
ff_free_expr(d);
return NULL;
}
@@ -273,7 +273,7 @@ static AVExpr * parse_primary(Parser *p) {
}
}
- *p->error = "unknown function";
+ av_log(p->log_ctx, AV_LOG_ERROR, "unknown function\n");
ff_free_expr(d);
return NULL;
}
@@ -373,7 +373,8 @@ AVExpr *ff_parse_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),
- const char **error){
+ void *log_ctx)
+{
Parser p;
AVExpr *e = NULL;
char *w = av_malloc(strlen(s) + 1);
@@ -393,7 +394,7 @@ AVExpr *ff_parse_expr(const char *s,
p.func1_name = func1_name;
p.func2 = func2;
p.func2_name = func2_name;
- p.error= error;
+ p.log_ctx = log_ctx;
e = parse_expr(&p);
if (!verify_expr(e)) {
@@ -417,8 +418,9 @@ double ff_parse_and_eval_expr(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),
- void *opaque, const char **error){
- AVExpr *e = ff_parse_expr(s, const_name, func1_name, func1, func2_name, func2, error);
+ void *opaque, void *log_ctx)
+{
+ AVExpr *e = ff_parse_expr(s, const_name, func1_name, func1, func2_name, func2, log_ctx);
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 0803397..fbf0437 100644
--- a/libavcodec/eval.h
+++ b/libavcodec/eval.h
@@ -40,14 +40,14 @@ typedef struct AVExpr AVExpr;
* @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 opaque a pointer which will be passed to all functions from func1 and func2
- * @param error pointer to a char* which is set to an error message if something goes wrong
+ * @param log_ctx logging context
* @return the value of the expression
*/
double ff_parse_and_eval_expr(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),
- void *opaque, const char **error);
+ void *opaque, void *log_ctx);
/**
* Parses an expression.
@@ -58,7 +58,7 @@ double ff_parse_and_eval_expr(const char *s,
* @param func1 NULL terminated array of function pointers for functions which take 1 argument
* @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 error pointer to a char* which is set to an error message if something goes wrong
+ * @param log_ctx 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
*/
@@ -66,7 +66,7 @@ AVExpr *ff_parse_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),
- const char **error);
+ void *log_ctx);
/**
* Evaluates a previously parsed expression.
diff --git a/libavcodec/opt.c b/libavcodec/opt.c
index f9cba05..844c0ec 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,9 @@ 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);
+ av_set_int(obj, "log_level_offset", AV_LOG_DEBUG - AV_LOG_QUIET);
+ d = ff_parse_and_eval_expr(buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, obj);
+ av_set_int(obj, "log_level_offset", 0);
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 +168,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 1c459d8..fa5dad2 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -31,6 +31,7 @@
#include "ratecontrol.h"
#include "mpegvideo.h"
#include "eval.h"
+#include "opt.h"
#undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
#include <assert.h>
@@ -67,7 +68,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 +107,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_names, func1, NULL, NULL, &error);
+ rcc->rc_eq_eval = ff_parse_expr(s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, s->avctx);
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
--/9DWx/yDrRhgMJTb--
More information about the ffmpeg-devel
mailing list