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_BOOL, {.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 };
90 formats = ff_make_format_list(sample_fmts);
91 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
94 layouts = ff_all_channel_layouts();
95 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
98 formats = ff_all_samplerates();
99 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
102 formats = ff_make_format_list(pix_fmts);
103 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
109 static int config_input(AVFilterLink *inlink)
111 AVFilterContext *ctx = inlink->dst;
112 ShowVolumeContext *s = ctx->priv;
115 nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
116 inlink->partial_buf_size =
117 inlink->min_samples =
118 inlink->max_samples = nb_samples;
123 static int config_output(AVFilterLink *outlink)
125 ShowVolumeContext *s = outlink->src->priv;
126 AVFilterLink *inlink = outlink->src->inputs[0];
129 outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
130 outlink->sample_aspect_ratio = (AVRational){1,1};
131 outlink->frame_rate = s->frame_rate;
136 static void drawtext(AVFrame *pic, int x, int y, const char *txt)
142 font = avpriv_cga_font, font_height = 8;
144 for (i = 0; txt[i]; i++) {
146 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
148 for (char_y = 0; char_y < font_height; char_y++) {
149 for (mask = 0x80; mask; mask >>= 1) {
150 if (font[txt[i] * font_height + char_y] & mask)
151 AV_WN32(p, ~AV_RN32(p));
154 p += pic->linesize[0] - 8*4;
159 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
161 AVFilterContext *ctx = inlink->dst;
162 AVFilterLink *outlink = ctx->outputs[0];
163 ShowVolumeContext *s = ctx->priv;
165 double values[VAR_VARS_NB];
167 if (!s->out || s->out->width != outlink->w ||
168 s->out->height != outlink->h) {
169 av_frame_free(&s->out);
170 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
172 av_frame_free(&insamples);
173 return AVERROR(ENOMEM);
176 for (i = 0; i < outlink->h; i++)
177 memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
179 s->out->pts = insamples->pts;
181 for (j = 0; j < outlink->h; j++) {
182 uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
183 for (k = 0; k < s->w; k++) {
184 dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] - s->f, 0);
185 dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] - s->f, 0);
186 dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] - s->f, 0);
187 dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] - s->f, 0);
191 for (c = 0; c < inlink->channels; c++) {
192 float *src = (float *)insamples->extended_data[c];
196 for (i = 0; i < insamples->nb_samples; i++)
197 max = FFMAX(max, src[i]);
199 max = av_clipf(max, 0, 1);
200 values[VAR_VOLUME] = 20.0 * log(max) / M_LN10;
201 values[VAR_CHANNEL] = c;
202 color = av_expr_eval(s->c_expr, values, NULL);
204 for (j = 0; j < s->h; j++) {
205 uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
207 for (k = 0; k < s->w * max; k++)
208 AV_WN32A(dst + k * 4, color);
211 if (s->h >= 8 && s->draw_text)
212 drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2,
213 av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c)));
216 av_frame_free(&insamples);
218 return ff_filter_frame(outlink, av_frame_clone(s->out));
221 static av_cold void uninit(AVFilterContext *ctx)
223 ShowVolumeContext *s = ctx->priv;
225 av_frame_free(&s->out);
226 av_expr_free(s->c_expr);
229 static const AVFilterPad showvolume_inputs[] = {
232 .type = AVMEDIA_TYPE_AUDIO,
233 .config_props = config_input,
234 .filter_frame = filter_frame,
239 static const AVFilterPad showvolume_outputs[] = {
242 .type = AVMEDIA_TYPE_VIDEO,
243 .config_props = config_output,
248 AVFilter ff_avf_showvolume = {
249 .name = "showvolume",
250 .description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
253 .query_formats = query_formats,
254 .priv_size = sizeof(ShowVolumeContext),
255 .inputs = showvolume_inputs,
256 .outputs = showvolume_outputs,
257 .priv_class = &showvolume_class,