]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showvolume.c
Merge commit 'c5fd4b50610f62cbb3baa4f4108139363128dea1'
[ffmpeg] / libavfilter / avf_showvolume.c
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 "formats.h"
30 #include "audio.h"
31 #include "video.h"
32 #include "internal.h"
33
34 static const char *const var_names[] = {   "VOLUME",   "CHANNEL",        NULL };
35 enum                                   { VAR_VOLUME, VAR_CHANNEL, VAR_VARS_NB };
36
37 typedef struct ShowVolumeContext {
38     const AVClass *class;
39     int w, h;
40     int b;
41     double f;
42     AVRational frame_rate;
43     char *color;
44     int orientation;
45     int step;
46
47     AVFrame *out;
48     AVExpr *c_expr;
49     int draw_text;
50     int draw_volume;
51     double *values;
52 } ShowVolumeContext;
53
54 #define OFFSET(x) offsetof(ShowVolumeContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56
57 static const AVOption showvolume_options[] = {
58     { "rate", "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
59     { "r",    "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
60     { "b", "set border width",   OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
61     { "w", "set channel width",  OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
62     { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
63     { "f", "set fade",           OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.001, 1, FLAGS },
64     { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-6), if(gte(VOLUME,-2), if(gte(VOLUME,-1), if(gt(VOLUME,0), 0xff0000ff, 0xff0066ff), 0xff00ffff),0xff00ff00),0xffff0000)"}, 0, 0, FLAGS },
65     { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
66     { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
67     { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
68     {   "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
69     {   "v", "vertical",   0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
70     { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
71     { NULL }
72 };
73
74 AVFILTER_DEFINE_CLASS(showvolume);
75
76 static av_cold int init(AVFilterContext *ctx)
77 {
78     ShowVolumeContext *s = ctx->priv;
79     int ret;
80
81     if (s->color) {
82         ret = av_expr_parse(&s->c_expr, s->color, var_names,
83                             NULL, NULL, NULL, NULL, 0, ctx);
84         if (ret < 0)
85             return ret;
86     }
87
88     return 0;
89 }
90
91 static int query_formats(AVFilterContext *ctx)
92 {
93     AVFilterFormats *formats = NULL;
94     AVFilterChannelLayouts *layouts = NULL;
95     AVFilterLink *inlink = ctx->inputs[0];
96     AVFilterLink *outlink = ctx->outputs[0];
97     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
98     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
99     int ret;
100
101     formats = ff_make_format_list(sample_fmts);
102     if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
103         return ret;
104
105     layouts = ff_all_channel_counts();
106     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
107         return ret;
108
109     formats = ff_all_samplerates();
110     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
111         return ret;
112
113     formats = ff_make_format_list(pix_fmts);
114     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
115         return ret;
116
117     return 0;
118 }
119
120 static int config_input(AVFilterLink *inlink)
121 {
122     AVFilterContext *ctx = inlink->dst;
123     ShowVolumeContext *s = ctx->priv;
124     int nb_samples;
125
126     nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
127     inlink->partial_buf_size =
128     inlink->min_samples =
129     inlink->max_samples = nb_samples;
130     s->values = av_calloc(inlink->channels * VAR_VARS_NB, sizeof(double));
131     if (!s->values)
132         return AVERROR(ENOMEM);
133     return 0;
134 }
135
136 static int config_output(AVFilterLink *outlink)
137 {
138     ShowVolumeContext *s = outlink->src->priv;
139     AVFilterLink *inlink = outlink->src->inputs[0];
140
141     if (s->orientation) {
142         outlink->h = s->w;
143         outlink->w = s->h * inlink->channels + (inlink->channels - 1) * s->b;
144     } else {
145         outlink->w = s->w;
146         outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
147     }
148
149     outlink->sample_aspect_ratio = (AVRational){1,1};
150     outlink->frame_rate = s->frame_rate;
151
152     return 0;
153 }
154
155 static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
156 {
157     const uint8_t *font;
158     int font_height;
159     int i;
160
161     font = avpriv_cga_font,   font_height =  8;
162
163     for (i = 0; txt[i]; i++) {
164         int char_y, mask;
165
166         if (o) {
167             for (char_y = font_height - 1; char_y >= 0; char_y--) {
168                 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
169                 for (mask = 0x80; mask; mask >>= 1) {
170                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
171                         AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
172                     p += pic->linesize[0];
173                 }
174             }
175         } else {
176             uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
177             for (char_y = 0; char_y < font_height; char_y++) {
178                 for (mask = 0x80; mask; mask >>= 1) {
179                     if (font[txt[i] * font_height + char_y] & mask)
180                         AV_WN32(p, ~AV_RN32(p));
181                     p += 4;
182                 }
183                 p += pic->linesize[0] - 8 * 4;
184             }
185         }
186     }
187 }
188
189 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
190 {
191     AVFilterContext *ctx = inlink->dst;
192     AVFilterLink *outlink = ctx->outputs[0];
193     ShowVolumeContext *s = ctx->priv;
194     const int step = s->step;
195     int c, i, j, k;
196     AVFrame *out;
197
198     if (!s->out || s->out->width  != outlink->w ||
199                    s->out->height != outlink->h) {
200         av_frame_free(&s->out);
201         s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
202         if (!s->out) {
203             av_frame_free(&insamples);
204             return AVERROR(ENOMEM);
205         }
206
207         for (i = 0; i < outlink->h; i++)
208             memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
209     }
210     s->out->pts = insamples->pts;
211
212     for (j = 0; j < outlink->h; j++) {
213         uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
214         for (k = 0; k < outlink->w; k++) {
215             dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
216             dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
217             dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
218             dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, 0);
219         }
220     }
221
222     if (s->orientation) {
223         for (c = 0; c < inlink->channels; c++) {
224             float *src = (float *)insamples->extended_data[c];
225             float max = 0;
226             uint32_t color;
227
228             for (i = 0; i < insamples->nb_samples; i++)
229                 max = FFMAX(max, src[i]);
230
231             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
232             max = av_clipf(max, 0, 1);
233             s->values[c * VAR_VARS_NB + VAR_CHANNEL] = c;
234             color = av_expr_eval(s->c_expr, &s->values[c * VAR_VARS_NB], NULL);
235
236             for (j = outlink->h - outlink->h * max; j < outlink->h; j++) {
237                 uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
238                 for (k = 0; k < s->h; k++) {
239                     AV_WN32A(&dst[k * 4], color);
240                     if (j & step)
241                         j += step;
242                 }
243             }
244
245             if (outlink->h > 40 && s->draw_text) {
246                 const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
247                 if (!channel_name)
248                     continue;
249                 drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
250             }
251         }
252     } else {
253         for (c = 0; c < inlink->channels; c++) {
254             float *src = (float *)insamples->extended_data[c];
255             float max = 0;
256             uint32_t color;
257
258             for (i = 0; i < insamples->nb_samples; i++)
259                 max = FFMAX(max, src[i]);
260
261             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
262             max = av_clipf(max, 0, 1);
263             s->values[c * VAR_VARS_NB + VAR_CHANNEL] = c;
264             color = av_expr_eval(s->c_expr, &s->values[c * VAR_VARS_NB], NULL);
265
266             for (j = 0; j < s->h; j++) {
267                 uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
268
269                 for (k = 0; k < s->w * max; k++) {
270                     AV_WN32A(dst + k * 4, color);
271                     if (k & step)
272                         k += step;
273                 }
274             }
275
276             if (s->h >= 8 && s->draw_text) {
277                 const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
278                 if (!channel_name)
279                     continue;
280                 drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
281             }
282         }
283     }
284
285     av_frame_free(&insamples);
286     out = av_frame_clone(s->out);
287     if (!out)
288         return AVERROR(ENOMEM);
289     av_frame_make_writable(out);
290
291     for (c = 0; c < inlink->channels && s->draw_volume; c++) {
292         char buf[16];
293         if (s->orientation) {
294             if (s->h >= 8) {
295                 snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
296                 drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
297             }
298         } else {
299             if (s->h >= 8) {
300                 snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
301                 drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
302             }
303         }
304     }
305
306     return ff_filter_frame(outlink, out);
307 }
308
309 static av_cold void uninit(AVFilterContext *ctx)
310 {
311     ShowVolumeContext *s = ctx->priv;
312
313     av_frame_free(&s->out);
314     av_expr_free(s->c_expr);
315     av_freep(&s->values);
316 }
317
318 static const AVFilterPad showvolume_inputs[] = {
319     {
320         .name         = "default",
321         .type         = AVMEDIA_TYPE_AUDIO,
322         .config_props = config_input,
323         .filter_frame = filter_frame,
324     },
325     { NULL }
326 };
327
328 static const AVFilterPad showvolume_outputs[] = {
329     {
330         .name         = "default",
331         .type         = AVMEDIA_TYPE_VIDEO,
332         .config_props = config_output,
333     },
334     { NULL }
335 };
336
337 AVFilter ff_avf_showvolume = {
338     .name          = "showvolume",
339     .description   = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
340     .init          = init,
341     .uninit        = uninit,
342     .query_formats = query_formats,
343     .priv_size     = sizeof(ShowVolumeContext),
344     .inputs        = showvolume_inputs,
345     .outputs       = showvolume_outputs,
346     .priv_class    = &showvolume_class,
347 };