From 06b19cff28558885b8c4a987d27f56a3a90528f7 Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Sat, 31 Mar 2018 15:52:20 +0200
Subject: [PATCH 4/5] avfilter/showvolume : add display scale option

linear (current behaviour) or log display (more close to classic audio meter)
---
 doc/filters.texi             |  4 ++++
 libavfilter/avf_showvolume.c | 22 +++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index bf2b94e240..764abd4be7 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -19974,6 +19974,10 @@ Set background opacity, allowed range is [0, 1]. Default is 0.
 @item m
 Set metering mode, can be peak: @code{p} or rms: @code{r},
 default is @code{p}.
+
+@item ds
+Set display scale, can be linear: @code{lin} or log: @code{log},
+default is @code{lin}.
 @end table
 
 @section showwaves
diff --git a/libavfilter/avf_showvolume.c b/libavfilter/avf_showvolume.c
index 7fe3916855..50ff80e022 100644
--- a/libavfilter/avf_showvolume.c
+++ b/libavfilter/avf_showvolume.c
@@ -54,6 +54,7 @@ typedef struct ShowVolumeContext {
     uint32_t *color_lut;
     float *max;
     float rms_factor;
+    int display_scale; /* 0 for linear, 1 for log */
 
     void (*meter)(float *src, int nb_samples, float *max, float factor);
 } ShowVolumeContext;
@@ -79,6 +80,9 @@ static const AVOption showvolume_options[] = {
     { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
     {   "p", "peak", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
     {   "r", "rms",  0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
+    { "ds", "set display scale", OFFSET(display_scale), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "display_scale" },
+    {   "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_scale" },
+    {   "log", "log",  0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_scale" },
     { NULL }
 };
 
@@ -262,7 +266,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     AVFilterLink *outlink = ctx->outputs[0];
     ShowVolumeContext *s = ctx->priv;
     const int step = s->step;
-    int c, j, k;
+    int c, j, k, max_draw;
     AVFrame *out;
 
     if (!s->out || s->out->width  != outlink->w ||
@@ -305,7 +309,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
             max = av_clipf(max, 0, 1);
 
-            for (j = outlink->h - outlink->h * max; j < s->w; j++) {
+            if (s->display_scale) { /* log display */
+                max_draw = -1 * (outlink->h * log10(max) * 0.5);
+            } else { /* linear */
+                max_draw = outlink->h - outlink->h * max;
+            }
+
+            for (j = max_draw; j < s->w; j++) {
                 uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
                 for (k = 0; k < s->h; k++) {
                     AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
@@ -333,10 +343,16 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
             max = av_clipf(max, 0, 1);
 
+            if (s->display_scale) { /* log display */
+                max_draw = s->w + (log10(max) * 0.5 * s->w);
+            } else { /* linear */
+                max_draw = s->w * max;
+            }
+
             for (j = 0; j < s->h; j++) {
                 uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
 
-                for (k = 0; k < s->w * max; k++) {
+                for (k = 0; k < max_draw; k++) {
                     AV_WN32A(dst + k * 4, lut[k]);
                     if (k & step)
                         k += step;
-- 
2.14.3 (Apple Git-98)

