From b438c2025c3e277bb506a3a7b8ec8a0d52ed3b07 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Tue, 16 Aug 2016 18:03:56 +0200
Subject: [PATCH] avfilter/window_func: add cauchy, parzen and poisson window
 function

---
 doc/filters.texi               |  9 +++++++
 libavfilter/avf_showfreqs.c    |  3 +++
 libavfilter/avf_showspectrum.c |  6 +++++
 libavfilter/window_func.c      | 44 ++++++++++++++++++++++++++++++++++
 libavfilter/window_func.h      |  3 ++-
 5 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 786a0caf949..5c48ad70229 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -16519,6 +16519,9 @@ It accepts the following values:
 @item gauss
 @item tukey
 @item dolph
+@item cauchy
+@item parzen
+@item poisson
 @end table
 Default is @code{hanning}.
 
@@ -16667,6 +16670,9 @@ It accepts the following values:
 @item gauss
 @item tukey
 @item dolph
+@item cauchy
+@item parzen
+@item poisson
 @end table
 
 Default value is @code{hann}.
@@ -16811,6 +16817,9 @@ It accepts the following values:
 @item gauss
 @item tukey
 @item dolph
+@item cauchy
+@item parzen
+@item poisson
 @end table
 Default value is @code{hann}.
 
diff --git a/libavfilter/avf_showfreqs.c b/libavfilter/avf_showfreqs.c
index 8cf8378ad43..6f9d486d139 100644
--- a/libavfilter/avf_showfreqs.c
+++ b/libavfilter/avf_showfreqs.c
@@ -113,6 +113,9 @@ static const AVOption showfreqs_options[] = {
         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
+        { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
+        { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
+        { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
     { "overlap",  "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
     { "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
     { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
index b4cc51b6522..567ce5d4830 100644
--- a/libavfilter/avf_showspectrum.c
+++ b/libavfilter/avf_showspectrum.c
@@ -135,6 +135,9 @@ static const AVOption showspectrum_options[] = {
         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
+        { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
+        { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
+        { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
     { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
         { "vertical",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL},   0, 0, FLAGS, "orientation" },
         { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
@@ -967,6 +970,9 @@ static const AVOption showspectrumpic_options[] = {
         { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
         { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
         { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
+        { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
+        { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
+        { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
     { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
         { "vertical",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL},   0, 0, FLAGS, "orientation" },
         { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
diff --git a/libavfilter/window_func.c b/libavfilter/window_func.c
index 1931c8ea653..fcdf6ea30e0 100644
--- a/libavfilter/window_func.c
+++ b/libavfilter/window_func.c
@@ -128,6 +128,50 @@ void ff_generate_window_func(float *lut, int N, int win_func, float *overlap)
         }
         *overlap = 0.5;}
         break;
+    case WFUNC_CAUCHY:
+        for (n = 0; n < N; n++) {
+            double x = 2 * ((n / (double)(N - 1)) - .5);
+
+            if (x <= -.5 || x >= .5) {
+                lut[n] = 0;
+            } else {
+                lut[n] = FFMIN(1, fabs(1/(1+4*16*x*x)));
+            }
+        }
+        *overlap = 0.75;
+        break;
+    case WFUNC_PARZEN:
+        for (n = 0; n < N; n++) {
+            double x = 2 * ((n / (double)(N - 1)) - .5);
+
+            if (x > 0.25 && x <= 0.5) {
+                lut[n] = -2 * powf(-1 + 2 * x, 3);
+            } else if (x >= -.5 && x < -.25) {
+                lut[n] = 2 * powf(1 + 2 * x, 3);
+            } else if (x >= -.25 && x < 0) {
+                lut[n] = 1 - 24 * x * x - 48 * x * x * x;
+            } else if (x >= 0 && x <= .25) {
+                lut[n] = 1 - 24 * x * x + 48 * x * x * x;
+            } else {
+                lut[n] = 0;
+            }
+        }
+        *overlap = 0.75;
+        break;
+    case WFUNC_POISSON:
+        for (n = 0; n < N; n++) {
+            double x = 2 * ((n / (double)(N - 1)) - .5);
+
+            if (x >= 0 && x <= .5) {
+                lut[n] = exp(-6*x);
+            } else if (x < 0 && x >= -.5) {
+                lut[n] = exp(6*x);
+            } else {
+                lut[n] = 0;
+            }
+        }
+        *overlap = 0.75;
+        break;
     default:
         av_assert0(0);
     }
diff --git a/libavfilter/window_func.h b/libavfilter/window_func.h
index 760020fc7f1..4611498d47b 100644
--- a/libavfilter/window_func.h
+++ b/libavfilter/window_func.h
@@ -26,7 +26,8 @@ enum WindowFunc     { WFUNC_RECT, WFUNC_HANNING, WFUNC_HAMMING, WFUNC_BLACKMAN,
                       WFUNC_BARTLETT, WFUNC_WELCH, WFUNC_FLATTOP,
                       WFUNC_BHARRIS, WFUNC_BNUTTALL, WFUNC_SINE, WFUNC_NUTTALL,
                       WFUNC_BHANN, WFUNC_LANCZOS, WFUNC_GAUSS, WFUNC_TUKEY,
-                      WFUNC_DOLPH, NB_WFUNC };
+                      WFUNC_DOLPH, WFUNC_CAUCHY, WFUNC_PARZEN, WFUNC_POISSON,
+                      NB_WFUNC };
 
 void ff_generate_window_func(float *lut, int N, int win_func, float *overlap);
 
-- 
GitLab