[FFmpeg-cvslog] lavu/eval: add clip function

Stefano Sabatini git at videolan.org
Thu Jul 17 13:27:11 CEST 2014


ffmpeg | branch: master | Stefano Sabatini <stefasab at gmail.com> | Fri Jul  4 14:13:11 2014 +0200| [f3e886c7dfcc6ca6e2b430b0978497da9136201d] | committer: Stefano Sabatini

lavu/eval: add clip function

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=f3e886c7dfcc6ca6e2b430b0978497da9136201d
---

 doc/utils.texi      |    3 +++
 libavutil/eval.c    |   14 +++++++++++++-
 libavutil/version.h |    2 +-
 tests/ref/fate/eval |    9 +++++++++
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/doc/utils.texi b/doc/utils.texi
index c46ad45..b0455af 100644
--- a/doc/utils.texi
+++ b/doc/utils.texi
@@ -782,6 +782,9 @@ large numbers (usually 2^53 and larger).
 Round the value of expression @var{expr} upwards to the nearest
 integer. For example, "ceil(1.5)" is "2.0".
 
+ at item clip(x, min, max)
+Return the value of @var{x} clipped between @var{min} and @var{max}.
+
 @item cos(x)
 Compute cosine of @var{x}.
 
diff --git a/libavutil/eval.c b/libavutil/eval.c
index 4a313bf..67b0a5f 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -147,7 +147,7 @@ struct AVExpr {
         e_pow, e_mul, e_div, e_add,
         e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc,
         e_sqrt, e_not, e_random, e_hypot, e_gcd,
-        e_if, e_ifnot, e_print, e_bitand, e_bitor, e_between,
+        e_if, e_ifnot, e_print, e_bitand, e_bitor, e_between, e_clip
     } type;
     double value; // is sign in other types
     union {
@@ -187,6 +187,13 @@ static double eval_expr(Parser *p, AVExpr *e)
                                           e->param[2] ? eval_expr(p, e->param[2]) : 0);
         case e_ifnot:  return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
                                           e->param[2] ? eval_expr(p, e->param[2]) : 0);
+        case e_clip: {
+            double x = eval_expr(p, e->param[0]);
+            double min = eval_expr(p, e->param[1]), max = eval_expr(p, e->param[2]);
+            if (isnan(min) || isnan(max) || isnan(x) || min > max)
+                return NAN;
+            return e->value * av_clipd(eval_expr(p, e->param[0]), min, max);
+        }
         case e_between: {
             double d = eval_expr(p, e->param[0]);
             return e->value * (d >= eval_expr(p, e->param[1]) &&
@@ -436,6 +443,7 @@ static int parse_primary(AVExpr **e, Parser *p)
     else if (strmatch(next, "bitand")) d->type = e_bitand;
     else if (strmatch(next, "bitor" )) d->type = e_bitor;
     else if (strmatch(next, "between"))d->type = e_between;
+    else if (strmatch(next, "clip"  )) d->type = e_clip;
     else {
         for (i=0; p->func1_names && p->func1_names[i]; i++) {
             if (strmatch(next, p->func1_names[i])) {
@@ -632,6 +640,7 @@ static int verify_expr(AVExpr *e)
             return verify_expr(e->param[0]) && verify_expr(e->param[1])
                    && (!e->param[2] || verify_expr(e->param[2]));
         case e_between:
+        case e_clip:
             return verify_expr(e->param[0]) &&
                    verify_expr(e->param[1]) &&
                    verify_expr(e->param[2]);
@@ -831,6 +840,9 @@ int main(int argc, char **argv)
         "between(10, -3, 10)",
         "between(-4, -2, -1)",
         "between(1,2)",
+        "clip(0, 2, 1)",
+        "clip(0/0, 1, 2)",
+        "clip(0, 0/0, 1)",
         NULL
     };
 
diff --git a/libavutil/version.h b/libavutil/version.h
index 0d254b1..6d8d6f0 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -57,7 +57,7 @@
 
 #define LIBAVUTIL_VERSION_MAJOR  52
 #define LIBAVUTIL_VERSION_MINOR  92
-#define LIBAVUTIL_VERSION_MICRO 100
+#define LIBAVUTIL_VERSION_MICRO 101
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
diff --git a/tests/ref/fate/eval b/tests/ref/fate/eval
index 97e0b20..914b13c 100644
--- a/tests/ref/fate/eval
+++ b/tests/ref/fate/eval
@@ -268,5 +268,14 @@ Evaluating 'between(-4, -2, -1)'
 Evaluating 'between(1,2)'
 'between(1,2)' -> nan
 
+Evaluating 'clip(0, 2, 1)'
+'clip(0, 2, 1)' -> nan
+
+Evaluating 'clip(0/0, 1, 2)'
+'clip(0/0, 1, 2)' -> nan
+
+Evaluating 'clip(0, 0/0, 1)'
+'clip(0, 0/0, 1)' -> nan
+
 12.700000 == 12.7
 0.931323 == 0.931322575



More information about the ffmpeg-cvslog mailing list