Browse code

lavfi/drawtext: add support to expansion of generic expressions

Stefano Sabatini authored on 2012/11/27 19:27:12
Showing 3 changed files
... ...
@@ -2075,6 +2075,16 @@ The following functions are available:
2075 2075
 
2076 2076
 @table @command
2077 2077
 
2078
+@item expr, e
2079
+The expression evaluation result.
2080
+
2081
+It must take one argument specifying the expression to be evaluated,
2082
+which accepts the same constants and functions as the @var{x} and
2083
+@var{y} values. Note that not all constants should be used, for
2084
+example the text size is not known when evaluating the expression, so
2085
+the constants @var{text_w} and @var{text_h} will have an undefined
2086
+value.
2087
+
2078 2088
 @item gmtime
2079 2089
 The time at which the filter is running, expressed in UTC.
2080 2090
 It can accept an argument: a strftime() format string.
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  23
33
-#define LIBAVFILTER_VERSION_MICRO 103
33
+#define LIBAVFILTER_VERSION_MICRO 104
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
... ...
@@ -643,12 +643,34 @@ static int func_strftime(AVFilterContext *ctx, AVBPrint *bp,
643 643
     return 0;
644 644
 }
645 645
 
646
+static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp,
647
+                          char *fct, unsigned argc, char **argv, int tag)
648
+{
649
+    DrawTextContext *dtext = ctx->priv;
650
+    double res;
651
+    int ret;
652
+
653
+    ret = av_expr_parse_and_eval(&res, argv[0], var_names, dtext->var_values,
654
+                                 NULL, NULL, fun2_names, fun2,
655
+                                 &dtext->prng, 0, ctx);
656
+    if (ret < 0)
657
+        av_log(ctx, AV_LOG_ERROR,
658
+               "Expression '%s' for the expr text expansion function is not valid\n",
659
+               argv[0]);
660
+    else
661
+        av_bprintf(bp, "%f", res);
662
+
663
+    return ret;
664
+}
665
+
646 666
 static const struct drawtext_function {
647 667
     const char *name;
648 668
     unsigned argc_min, argc_max;
649 669
     int tag; /** opaque argument to func */
650 670
     int (*func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int);
651 671
 } functions[] = {
672
+    { "expr",      1, 1, 0,   func_eval_expr },
673
+    { "e",         1, 1, 0,   func_eval_expr },
652 674
     { "pts",       0, 0, 0,   func_pts      },
653 675
     { "gmtime",    0, 1, 'G', func_strftime },
654 676
     { "localtime", 0, 1, 'L', func_strftime },