• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avstring.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/xga_font_data.h"
28 #include "avfilter.h"
29 #include "filters.h"
30 #include "formats.h"
31 #include "audio.h"
32 #include "video.h"
33 #include "internal.h"
34 
35 static const char *const var_names[] = {   "VOLUME",   "CHANNEL",   "PEAK",        NULL };
36 enum                                   { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
37 enum DisplayScale   { LINEAR, LOG, NB_DISPLAY_SCALE };
38 
39 typedef struct ShowVolumeContext {
40     const AVClass *class;
41     int w, h;
42     int b;
43     double f;
44     AVRational frame_rate;
45     char *color;
46     int orientation;
47     int step;
48     float bgopacity;
49     int mode;
50 
51     int nb_samples;
52     AVFrame *out;
53     AVExpr *c_expr;
54     int draw_text;
55     int draw_volume;
56     double *values;
57     uint32_t *color_lut;
58     float *max;
59     float rms_factor;
60     int display_scale;
61 
62     double draw_persistent_duration; /* in second */
63     uint8_t persistant_max_rgba[4];
64     int persistent_max_frames; /* number of frames to check max value */
65     float *max_persistent; /* max value for draw_persistent_max for each channel */
66     int *nb_frames_max_display; /* number of frame for each channel, for displaying the max value */
67 
68     void (*meter)(float *src, int nb_samples, float *max, float factor);
69 } ShowVolumeContext;
70 
71 #define OFFSET(x) offsetof(ShowVolumeContext, x)
72 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73 
74 static const AVOption showvolume_options[] = {
75     { "rate", "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
76     { "r",    "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
77     { "b", "set border width",   OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
78     { "w", "set channel width",  OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
79     { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
80     { "f", "set fade",           OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0, 1, FLAGS },
81     { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS },
82     { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
83     { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
84     { "dm", "duration for max value display", OFFSET(draw_persistent_duration), AV_OPT_TYPE_DOUBLE, {.dbl=0.}, 0, 9000, FLAGS},
85     { "dmc","set color of the max value line", OFFSET(persistant_max_rgba), AV_OPT_TYPE_COLOR, {.str = "orange"}, 0, 0, FLAGS },
86     { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
87     {   "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
88     {   "v", "vertical",   0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
89     { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
90     { "p", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS },
91     { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
92     {   "p", "peak", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
93     {   "r", "rms",  0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
94     { "ds", "set display scale", OFFSET(display_scale), AV_OPT_TYPE_INT, {.i64=LINEAR}, LINEAR, NB_DISPLAY_SCALE - 1, FLAGS, "display_scale" },
95     {   "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "display_scale" },
96     {   "log", "log",  0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "display_scale" },
97     { NULL }
98 };
99 
100 AVFILTER_DEFINE_CLASS(showvolume);
101 
init(AVFilterContext * ctx)102 static av_cold int init(AVFilterContext *ctx)
103 {
104     ShowVolumeContext *s = ctx->priv;
105     int ret;
106 
107     if (s->color) {
108         ret = av_expr_parse(&s->c_expr, s->color, var_names,
109                             NULL, NULL, NULL, NULL, 0, ctx);
110         if (ret < 0)
111             return ret;
112     }
113 
114     return 0;
115 }
116 
query_formats(AVFilterContext * ctx)117 static int query_formats(AVFilterContext *ctx)
118 {
119     AVFilterFormats *formats = NULL;
120     AVFilterChannelLayouts *layouts = NULL;
121     AVFilterLink *inlink = ctx->inputs[0];
122     AVFilterLink *outlink = ctx->outputs[0];
123     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
124     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
125     int ret;
126 
127     formats = ff_make_format_list(sample_fmts);
128     if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
129         return ret;
130 
131     layouts = ff_all_channel_counts();
132     if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
133         return ret;
134 
135     formats = ff_all_samplerates();
136     if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
137         return ret;
138 
139     formats = ff_make_format_list(pix_fmts);
140     if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
141         return ret;
142 
143     return 0;
144 }
145 
find_peak(float * src,int nb_samples,float * peak,float factor)146 static void find_peak(float *src, int nb_samples, float *peak, float factor)
147 {
148     int i;
149 
150     *peak = 0;
151     for (i = 0; i < nb_samples; i++)
152         *peak = FFMAX(*peak, FFABS(src[i]));
153 }
154 
find_rms(float * src,int nb_samples,float * rms,float factor)155 static void find_rms(float *src, int nb_samples, float *rms, float factor)
156 {
157     int i;
158 
159     for (i = 0; i < nb_samples; i++)
160         *rms += factor * (src[i] * src[i] - *rms);
161 }
162 
config_input(AVFilterLink * inlink)163 static int config_input(AVFilterLink *inlink)
164 {
165     AVFilterContext *ctx = inlink->dst;
166     ShowVolumeContext *s = ctx->priv;
167 
168     s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
169     s->values = av_calloc(inlink->ch_layout.nb_channels * VAR_VARS_NB, sizeof(double));
170     if (!s->values)
171         return AVERROR(ENOMEM);
172 
173     s->color_lut = av_calloc(s->w, sizeof(*s->color_lut) * inlink->ch_layout.nb_channels);
174     if (!s->color_lut)
175         return AVERROR(ENOMEM);
176 
177     s->max = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->max));
178     if (!s->max)
179         return AVERROR(ENOMEM);
180 
181     s->rms_factor = 10000. / inlink->sample_rate;
182 
183     switch (s->mode) {
184     case 0: s->meter = find_peak; break;
185     case 1: s->meter = find_rms;  break;
186     default: return AVERROR_BUG;
187     }
188 
189     if (s->draw_persistent_duration > 0.) {
190         s->persistent_max_frames = (int) FFMAX(av_q2d(s->frame_rate) * s->draw_persistent_duration, 1.);
191         s->max_persistent = av_calloc(inlink->ch_layout.nb_channels * s->persistent_max_frames, sizeof(*s->max_persistent));
192         s->nb_frames_max_display = av_calloc(inlink->ch_layout.nb_channels * s->persistent_max_frames, sizeof(*s->nb_frames_max_display));
193         if (!s->max_persistent ||
194             !s->nb_frames_max_display)
195             return AVERROR(ENOMEM);
196     }
197     return 0;
198 }
199 
config_output(AVFilterLink * outlink)200 static int config_output(AVFilterLink *outlink)
201 {
202     ShowVolumeContext *s = outlink->src->priv;
203     AVFilterLink *inlink = outlink->src->inputs[0];
204     int ch;
205 
206     if (s->orientation) {
207         outlink->h = s->w;
208         outlink->w = s->h * inlink->ch_layout.nb_channels + (inlink->ch_layout.nb_channels - 1) * s->b;
209     } else {
210         outlink->w = s->w;
211         outlink->h = s->h * inlink->ch_layout.nb_channels + (inlink->ch_layout.nb_channels - 1) * s->b;
212     }
213 
214     outlink->sample_aspect_ratio = (AVRational){1,1};
215     outlink->frame_rate = s->frame_rate;
216     outlink->time_base = av_inv_q(outlink->frame_rate);
217 
218     for (ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
219         int i;
220 
221         for (i = 0; i < s->w; i++) {
222             float max = i / (float)(s->w - 1);
223 
224             s->values[ch * VAR_VARS_NB + VAR_PEAK] = max;
225             s->values[ch * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
226             s->values[ch * VAR_VARS_NB + VAR_CHANNEL] = ch;
227             s->color_lut[ch * s->w + i] = av_expr_eval(s->c_expr, &s->values[ch * VAR_VARS_NB], NULL);
228         }
229     }
230 
231     return 0;
232 }
233 
drawtext(AVFrame * pic,int x,int y,const char * txt,int o)234 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
235 {
236     const uint8_t *font;
237     int font_height;
238     int i;
239 
240     font = avpriv_cga_font,   font_height =  8;
241 
242     for (i = 0; txt[i]; i++) {
243         int char_y, mask;
244 
245         if (o) { /* vertical orientation */
246             for (char_y = font_height - 1; char_y >= 0; char_y--) {
247                 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
248                 for (mask = 0x80; mask; mask >>= 1) {
249                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
250                         AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
251                     p += pic->linesize[0];
252                 }
253             }
254         } else { /* horizontal orientation */
255             uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
256             for (char_y = 0; char_y < font_height; char_y++) {
257                 for (mask = 0x80; mask; mask >>= 1) {
258                     if (font[txt[i] * font_height + char_y] & mask)
259                         AV_WN32(p, ~AV_RN32(p));
260                     p += 4;
261                 }
262                 p += pic->linesize[0] - 8 * 4;
263             }
264         }
265     }
266 }
267 
clear_picture(ShowVolumeContext * s,AVFilterLink * outlink)268 static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink)
269 {
270     int i, j;
271     const uint32_t bg = (uint32_t)(s->bgopacity * 255) << 24;
272 
273     for (i = 0; i < outlink->h; i++) {
274         uint32_t *dst = (uint32_t *)(s->out->data[0] + i * s->out->linesize[0]);
275         for (j = 0; j < outlink->w; j++)
276             AV_WN32A(dst + j, bg);
277     }
278 }
279 
calc_max_draw(ShowVolumeContext * s,AVFilterLink * outlink,float max)280 static inline int calc_max_draw(ShowVolumeContext *s, AVFilterLink *outlink, float max)
281 {
282     float max_val;
283     if (s->display_scale == LINEAR) {
284         max_val = max;
285     } else { /* log */
286         max_val = av_clipf(0.21 * log10(max) + 1, 0, 1);
287     }
288     if (s->orientation) { /* vertical */
289         return outlink->h - outlink->h * max_val;
290     } else { /* horizontal */
291         return s->w * max_val;
292     }
293 }
294 
calc_persistent_max(ShowVolumeContext * s,float max,int channel)295 static inline void calc_persistent_max(ShowVolumeContext *s, float max, int channel)
296 {
297     /* update max value for persistent max display */
298     if ((max >= s->max_persistent[channel]) || (s->nb_frames_max_display[channel] >= s->persistent_max_frames)) { /* update max value for display */
299         s->max_persistent[channel] = max;
300         s->nb_frames_max_display[channel] = 0;
301     } else {
302         s->nb_frames_max_display[channel] += 1; /* incremente display frame count */
303     }
304 }
305 
draw_max_line(ShowVolumeContext * s,int max_draw,int channel)306 static inline void draw_max_line(ShowVolumeContext *s, int max_draw, int channel)
307 {
308     int k;
309     if (s->orientation) { /* vertical */
310         uint8_t *dst = s->out->data[0] + max_draw * s->out->linesize[0] + channel * (s->b + s->h) * 4;
311         for (k = 0; k < s->h; k++) {
312             memcpy(dst + k * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
313         }
314     } else { /* horizontal */
315         for (k = 0; k < s->h; k++) {
316             uint8_t *dst = s->out->data[0] + (channel * s->h + channel * s->b + k) * s->out->linesize[0];
317             memcpy(dst + max_draw * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
318         }
319     }
320 }
321 
filter_frame(AVFilterLink * inlink,AVFrame * insamples)322 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
323 {
324     AVFilterContext *ctx = inlink->dst;
325     AVFilterLink *outlink = ctx->outputs[0];
326     ShowVolumeContext *s = ctx->priv;
327     const int step = s->step;
328     int c, j, k, max_draw;
329     char channel_name[64];
330     AVFrame *out;
331 
332     if (!s->out || s->out->width  != outlink->w ||
333                    s->out->height != outlink->h) {
334         av_frame_free(&s->out);
335         s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
336         if (!s->out) {
337             av_frame_free(&insamples);
338             return AVERROR(ENOMEM);
339         }
340         clear_picture(s, outlink);
341     }
342     s->out->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
343 
344     if ((s->f < 1.) && (s->f > 0.)) {
345         for (j = 0; j < outlink->h; j++) {
346             uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
347             const uint32_t alpha = s->bgopacity * 255;
348 
349             for (k = 0; k < outlink->w; k++) {
350                 dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
351                 dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
352                 dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
353                 dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, alpha);
354             }
355         }
356     } else if (s->f == 0.) {
357         clear_picture(s, outlink);
358     }
359 
360     if (s->orientation) { /* vertical */
361         for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
362             float *src = (float *)insamples->extended_data[c];
363             uint32_t *lut = s->color_lut + s->w * c;
364             float max;
365 
366             s->meter(src, insamples->nb_samples, &s->max[c], s->rms_factor);
367             max = s->max[c];
368 
369             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
370             max = av_clipf(max, 0, 1);
371             max_draw = calc_max_draw(s, outlink, max);
372 
373             for (j = max_draw; j < s->w; j++) {
374                 uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
375                 for (k = 0; k < s->h; k++) {
376                     AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
377                     if (j & step)
378                         j += step;
379                 }
380             }
381 
382             if (s->h >= 8 && s->draw_text) {
383                 int ret = av_channel_name(channel_name, sizeof(channel_name), av_channel_layout_channel_from_index(&insamples->ch_layout, c));
384                 if (ret < 0)
385                     continue;
386                 drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
387             }
388 
389             if (s->draw_persistent_duration > 0.) {
390                 calc_persistent_max(s, max, c);
391                 max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
392                 draw_max_line(s, max_draw, c);
393             }
394         }
395     } else { /* horizontal */
396         for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
397             float *src = (float *)insamples->extended_data[c];
398             uint32_t *lut = s->color_lut + s->w * c;
399             float max;
400 
401             s->meter(src, insamples->nb_samples, &s->max[c], s->rms_factor);
402             max = s->max[c];
403 
404             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
405             max = av_clipf(max, 0, 1);
406             max_draw = calc_max_draw(s, outlink, max);
407 
408             for (j = 0; j < s->h; j++) {
409                 uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
410 
411                 for (k = 0; k < max_draw; k++) {
412                     AV_WN32A(dst + k * 4, lut[k]);
413                     if (k & step)
414                         k += step;
415                 }
416             }
417 
418             if (s->h >= 8 && s->draw_text) {
419                 int ret = av_channel_name(channel_name, sizeof(channel_name), av_channel_layout_channel_from_index(&insamples->ch_layout, c));
420                 if (ret < 0)
421                     continue;
422                 drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
423             }
424 
425             if (s->draw_persistent_duration > 0.) {
426                 calc_persistent_max(s, max, c);
427                 max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
428                 draw_max_line(s, max_draw, c);
429             }
430         }
431     }
432 
433     av_frame_free(&insamples);
434     out = av_frame_clone(s->out);
435     if (!out)
436         return AVERROR(ENOMEM);
437     av_frame_make_writable(out);
438 
439     /* draw volume level */
440     for (c = 0; c < inlink->ch_layout.nb_channels && s->h >= 8 && s->draw_volume; c++) {
441         char buf[16];
442 
443         if (s->orientation) { /* vertical */
444             snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
445             drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
446         } else { /* horizontal */
447             snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
448             drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
449         }
450     }
451 
452     return ff_filter_frame(outlink, out);
453 }
454 
activate(AVFilterContext * ctx)455 static int activate(AVFilterContext *ctx)
456 {
457     AVFilterLink *inlink = ctx->inputs[0];
458     AVFilterLink *outlink = ctx->outputs[0];
459     ShowVolumeContext *s = ctx->priv;
460     AVFrame *in = NULL;
461     int ret;
462 
463     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
464 
465     ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
466     if (ret < 0)
467         return ret;
468     if (ret > 0)
469         return filter_frame(inlink, in);
470 
471     if (ff_inlink_queued_samples(inlink) >= s->nb_samples) {
472         ff_filter_set_ready(ctx, 10);
473         return 0;
474     }
475 
476     FF_FILTER_FORWARD_STATUS(inlink, outlink);
477     FF_FILTER_FORWARD_WANTED(outlink, inlink);
478 
479     return FFERROR_NOT_READY;
480 }
481 
uninit(AVFilterContext * ctx)482 static av_cold void uninit(AVFilterContext *ctx)
483 {
484     ShowVolumeContext *s = ctx->priv;
485 
486     av_frame_free(&s->out);
487     av_expr_free(s->c_expr);
488     av_freep(&s->values);
489     av_freep(&s->color_lut);
490     av_freep(&s->max);
491     av_freep(&s->max_persistent);
492     av_freep(&s->nb_frames_max_display);
493 }
494 
495 static const AVFilterPad showvolume_inputs[] = {
496     {
497         .name         = "default",
498         .type         = AVMEDIA_TYPE_AUDIO,
499         .config_props = config_input,
500     },
501 };
502 
503 static const AVFilterPad showvolume_outputs[] = {
504     {
505         .name         = "default",
506         .type         = AVMEDIA_TYPE_VIDEO,
507         .config_props = config_output,
508     },
509 };
510 
511 const AVFilter ff_avf_showvolume = {
512     .name          = "showvolume",
513     .description   = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
514     .init          = init,
515     .activate      = activate,
516     .uninit        = uninit,
517     .priv_size     = sizeof(ShowVolumeContext),
518     FILTER_INPUTS(showvolume_inputs),
519     FILTER_OUTPUTS(showvolume_outputs),
520     FILTER_QUERY_FUNC(query_formats),
521     .priv_class    = &showvolume_class,
522 };
523