2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/channel_layout.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/xga_font_data.h"
33 typedef struct ShowVolumeContext {
37 AVRational frame_rate;
45 #define OFFSET(x) offsetof(ShowVolumeContext, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
48 static const AVOption showvolume_options[] = {
49 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
50 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
51 { "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
52 { "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 40, 1080, FLAGS },
53 { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 100, FLAGS },
54 { "f", "set fade", OFFSET(f), AV_OPT_TYPE_INT, {.i64=20}, 1, 255, FLAGS },
55 { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-2), if(gte(VOLUME,-1),0xff0000ff, 0xff00ffff),0xff00ff00)"}, 0, 0, FLAGS },
56 { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
60 AVFILTER_DEFINE_CLASS(showvolume);
62 static const char *const var_names[] = { "VOLUME", "CHANNEL", NULL };
63 enum { VAR_VOLUME, VAR_CHANNEL, VAR_VARS_NB };
65 static av_cold int init(AVFilterContext *ctx)
67 ShowVolumeContext *s = ctx->priv;
71 ret = av_expr_parse(&s->c_expr, s->color, var_names,
72 NULL, NULL, NULL, NULL, 0, ctx);
80 static int query_formats(AVFilterContext *ctx)
82 AVFilterFormats *formats = NULL;
83 AVFilterChannelLayouts *layouts = NULL;
84 AVFilterLink *inlink = ctx->inputs[0];
85 AVFilterLink *outlink = ctx->outputs[0];
86 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
87 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
89 formats = ff_make_format_list(sample_fmts);
91 return AVERROR(ENOMEM);
92 ff_formats_ref(formats, &inlink->out_formats);
94 layouts = ff_all_channel_layouts();
96 return AVERROR(ENOMEM);
97 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
99 formats = ff_all_samplerates();
101 return AVERROR(ENOMEM);
102 ff_formats_ref(formats, &inlink->out_samplerates);
104 formats = ff_make_format_list(pix_fmts);
106 return AVERROR(ENOMEM);
107 ff_formats_ref(formats, &outlink->in_formats);
112 static int config_input(AVFilterLink *inlink)
114 AVFilterContext *ctx = inlink->dst;
115 ShowVolumeContext *s = ctx->priv;
118 nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
119 inlink->partial_buf_size =
120 inlink->min_samples =
121 inlink->max_samples = nb_samples;
126 static int config_output(AVFilterLink *outlink)
128 ShowVolumeContext *s = outlink->src->priv;
129 AVFilterLink *inlink = outlink->src->inputs[0];
132 outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
133 outlink->sample_aspect_ratio = (AVRational){1,1};
134 outlink->frame_rate = s->frame_rate;
139 static void drawtext(AVFrame *pic, int x, int y, const char *txt)
145 font = avpriv_cga_font, font_height = 8;
147 for (i = 0; txt[i]; i++) {
149 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
151 for (char_y = 0; char_y < font_height; char_y++) {
152 for (mask = 0x80; mask; mask >>= 1) {
153 if (font[txt[i] * font_height + char_y] & mask)
154 AV_WN32(p, ~AV_RN32(p));
157 p += pic->linesize[0] - 8*4;
162 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
164 AVFilterContext *ctx = inlink->dst;
165 AVFilterLink *outlink = ctx->outputs[0];
166 ShowVolumeContext *s = ctx->priv;
168 double values[VAR_VARS_NB];
170 if (!s->out || s->out->width != outlink->w ||
171 s->out->height != outlink->h) {
172 av_frame_free(&s->out);
173 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
175 av_frame_free(&insamples);
176 return AVERROR(ENOMEM);
179 for (i = 0; i < outlink->h; i++)
180 memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
182 s->out->pts = insamples->pts;
184 for (j = 0; j < outlink->h; j++) {
185 uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
186 for (k = 0; k < s->w; k++) {
187 dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] - s->f, 0);
188 dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] - s->f, 0);
189 dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] - s->f, 0);
190 dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] - s->f, 0);
194 for (c = 0; c < inlink->channels; c++) {
195 float *src = (float *)insamples->extended_data[c];
199 for (i = 0; i < insamples->nb_samples; i++)
200 max = FFMAX(max, src[i]);
202 max = av_clipf(max, 0, 1);
203 values[VAR_VOLUME] = 20.0 * log(max) / M_LN10;
204 values[VAR_CHANNEL] = c;
205 color = av_expr_eval(s->c_expr, values, NULL);
207 for (j = 0; j < s->h; j++) {
208 uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
210 for (k = 0; k < s->w * max; k++)
211 AV_WN32A(dst + k * 4, color);
214 if (s->h >= 8 && s->draw_text)
215 drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2,
216 av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c)));
219 av_frame_free(&insamples);
221 return ff_filter_frame(outlink, av_frame_clone(s->out));
224 static av_cold void uninit(AVFilterContext *ctx)
226 ShowVolumeContext *s = ctx->priv;
228 av_frame_free(&s->out);
229 av_expr_free(s->c_expr);
232 static const AVFilterPad showvolume_inputs[] = {
235 .type = AVMEDIA_TYPE_AUDIO,
236 .config_props = config_input,
237 .filter_frame = filter_frame,
242 static const AVFilterPad showvolume_outputs[] = {
245 .type = AVMEDIA_TYPE_VIDEO,
246 .config_props = config_output,
251 AVFilter ff_avf_showvolume = {
252 .name = "showvolume",
253 .description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
256 .query_formats = query_formats,
257 .priv_size = sizeof(ShowVolumeContext),
258 .inputs = showvolume_inputs,
259 .outputs = showvolume_outputs,
260 .priv_class = &showvolume_class,