]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/avf_showvolume.c
Merge commit 'ea8ae27a5e112d06fd5625f640e40849e6313f0c'
[ffmpeg] / libavfilter / avf_showvolume.c
index 7fe39168555b68ecb90f8f32ee35ce41292db638..6b553c4eba77478082fd401ca8153541aae410e2 100644 (file)
@@ -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,13 @@ typedef struct ShowVolumeContext {
     uint32_t *color_lut;
     float *max;
     float rms_factor;
+    int display_scale;
+
+    double draw_persistent_duration; /* in second */
+    uint8_t persistant_max_rgba[4];
+    int persistent_max_frames; /* number of frames to check max value */
+    float *max_persistent; /* max value for draw_persistent_max for each channel */
+    int *nb_frames_max_display; /* number of frame for each channel, for displaying the max value */
 
     void (*meter)(float *src, int nb_samples, float *max, float factor);
 } ShowVolumeContext;
@@ -71,6 +79,8 @@ static const AVOption showvolume_options[] = {
     { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS },
     { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
     { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
+    { "dm", "duration for max value display", OFFSET(draw_persistent_duration), AV_OPT_TYPE_DOUBLE, {.dbl=0.}, 0, 9000, FLAGS},
+    { "dmc","set color of the max value line", OFFSET(persistant_max_rgba), AV_OPT_TYPE_COLOR, {.str = "orange"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
     {   "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
     {   "v", "vertical",   0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
@@ -79,6 +89,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 }
 };
 
@@ -175,6 +188,11 @@ static int config_input(AVFilterLink *inlink)
     default: return AVERROR_BUG;
     }
 
+    if (s->draw_persistent_duration > 0.) {
+        s->persistent_max_frames = (int) FFMAX(av_q2d(s->frame_rate) * s->draw_persistent_duration, 1.);
+        s->max_persistent = av_calloc(inlink->channels * s->persistent_max_frames, sizeof(*s->max_persistent));
+        s->nb_frames_max_display = av_calloc(inlink->channels * s->persistent_max_frames, sizeof(*s->nb_frames_max_display));
+    }
     return 0;
 }
 
@@ -245,7 +263,8 @@ static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
     }
 }
 
-static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink) {
+static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink)
+{
     int i, j;
     const uint32_t bg = (uint32_t)(s->bgopacity * 255) << 24;
 
@@ -256,13 +275,55 @@ 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 inline void calc_persistent_max(ShowVolumeContext *s, float max, int channel)
+{
+    /* update max value for persistent max display */
+    if ((max >= s->max_persistent[channel]) || (s->nb_frames_max_display[channel] >= s->persistent_max_frames)) { /* update max value for display */
+        s->max_persistent[channel] = max;
+        s->nb_frames_max_display[channel] = 0;
+    } else {
+        s->nb_frames_max_display[channel] += 1; /* incremente display frame count */
+    }
+}
+
+static inline void draw_max_line(ShowVolumeContext *s, int max_draw, int channel)
+{
+    int k;
+    if (s->orientation) { /* vertical */
+        uint8_t *dst = s->out->data[0] + max_draw * s->out->linesize[0] + channel * (s->b + s->h) * 4;
+        for (k = 0; k < s->h; k++) {
+            memcpy(dst + k * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
+        }
+    } else { /* horizontal */
+        for (k = 0; k < s->h; k++) {
+            uint8_t *dst = s->out->data[0] + (channel * s->h + channel * s->b + k) * s->out->linesize[0];
+            memcpy(dst + max_draw * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
+        }
+    }
+}
+
 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 +365,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]);
@@ -320,6 +382,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
                     continue;
                 drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
             }
+
+            if (s->draw_persistent_duration > 0.) {
+                calc_persistent_max(s, max, c);
+                max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
+                draw_max_line(s, max_draw, c);
+            }
         }
     } else { /* horizontal */
         for (c = 0; c < inlink->channels; c++) {
@@ -332,11 +400,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;
@@ -349,6 +418,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
                     continue;
                 drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
             }
+
+            if (s->draw_persistent_duration > 0.) {
+                calc_persistent_max(s, max, c);
+                max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
+                draw_max_line(s, max_draw, c);
+            }
         }
     }