[FFmpeg-cvslog] avfilter/showvolume : add display scale option

Martin Vignali git at videolan.org
Mon Apr 16 12:36:13 EEST 2018


ffmpeg | branch: master | Martin Vignali <martin.vignali at gmail.com> | Mon Apr 16 11:31:43 2018 +0200| [cb2f52b6a70971c902ea2aaa989d141973472125] | committer: Martin Vignali

avfilter/showvolume : add display scale option

linear (current behaviour) or log display (more close to classic audio meter)

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

 doc/filters.texi             |  4 ++++
 libavfilter/avf_showvolume.c | 28 +++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 40083dd080..d95b377bd7 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -20014,6 +20014,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}.
+
+ at 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..da8b4088bd 100644
--- a/libavfilter/avf_showvolume.c
+++ b/libavfilter/avf_showvolume.c
@@ -33,6 +33,7 @@
 
 static const char *const var_names[] = {   "VOLUME",   "CHANNEL",   "PEAK",        NULL };
 enum                                   { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
+enum DisplayScale   { LINEAR, LOG, NB_DISPLAY_SCALE };
 
 typedef struct ShowVolumeContext {
     const AVClass *class;
@@ -54,6 +55,7 @@ typedef struct ShowVolumeContext {
     uint32_t *color_lut;
     float *max;
     float rms_factor;
+    int display_scale;
 
     void (*meter)(float *src, int nb_samples, float *max, float factor);
 } ShowVolumeContext;
@@ -79,6 +81,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=LINEAR}, LINEAR, NB_DISPLAY_SCALE - 1, FLAGS, "display_scale" },
+    {   "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "display_scale" },
+    {   "log", "log",  0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "display_scale" },
     { NULL }
 };
 
@@ -256,13 +261,28 @@ static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink) {
     }
 }
 
+static inline int calc_max_draw(ShowVolumeContext *s, AVFilterLink *outlink, float max)
+{
+    float max_val;
+    if (s->display_scale == LINEAR) {
+        max_val = max;
+    } else { /* log */
+        max_val = av_clipf(0.21 * log10(max) + 1, 0, 1);
+    }
+    if (s->orientation) { /* vertical */
+        return outlink->h - outlink->h * max_val;
+    } else { /* horizontal */
+        return s->w * max_val;
+    }
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     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 ||
@@ -304,8 +324,9 @@ 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);
+            max_draw = calc_max_draw(s, outlink, max);
 
-            for (j = outlink->h - outlink->h * max; j < s->w; j++) {
+            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]);
@@ -332,11 +353,12 @@ 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);
+            max_draw = calc_max_draw(s, outlink, 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;



More information about the ffmpeg-cvslog mailing list