1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
* Copyright (c) 2015 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/channel_layout.h"
#include "libavutil/eval.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
#include "libavutil/xga_font_data.h"
#include "avfilter.h"
#include "formats.h"
#include "audio.h"
#include "video.h"
#include "internal.h"
typedef struct ShowVolumeContext {
const AVClass *class;
int w, h;
int f, b;
AVRational frame_rate;
char *color;
AVFrame *out;
AVExpr *c_expr;
int draw_text;
} ShowVolumeContext;
#define OFFSET(x) offsetof(ShowVolumeContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
static const AVOption showvolume_options[] = {
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
{ "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
{ "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
{ "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 40, 1080, FLAGS },
{ "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 100, FLAGS },
{ "f", "set fade", OFFSET(f), AV_OPT_TYPE_INT, {.i64=20}, 1, 255, FLAGS },
{ "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-2), if(gte(VOLUME,-1),0xff, 0xffff),0xff00)"}, 0, 0, FLAGS },
{ "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
{ NULL }
};
AVFILTER_DEFINE_CLASS(showvolume);
static const char *const var_names[] = { "VOLUME", "CHANNEL", NULL };
enum { VAR_VOLUME, VAR_CHANNEL, VAR_VARS_NB };
static av_cold int init(AVFilterContext *ctx)
{
ShowVolumeContext *s = ctx->priv;
int ret;
if (s->color) {
ret = av_expr_parse(&s->c_expr, s->color, var_names,
NULL, NULL, NULL, NULL, 0, ctx);
if (ret < 0)
return ret;
}
return 0;
}
static int query_formats(AVFilterContext *ctx)
{
AVFilterFormats *formats = NULL;
AVFilterChannelLayouts *layouts = NULL;
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
formats = ff_make_format_list(sample_fmts);
if (!formats)
return AVERROR(ENOMEM);
ff_formats_ref(formats, &inlink->out_formats);
layouts = ff_all_channel_layouts();
if (!layouts)
return AVERROR(ENOMEM);
ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
formats = ff_all_samplerates();
if (!formats)
return AVERROR(ENOMEM);
ff_formats_ref(formats, &inlink->out_samplerates);
formats = ff_make_format_list(pix_fmts);
if (!formats)
return AVERROR(ENOMEM);
ff_formats_ref(formats, &outlink->in_formats);
return 0;
}
static int config_input(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst;
ShowVolumeContext *s = ctx->priv;
int nb_samples;
nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
inlink->partial_buf_size =
inlink->min_samples =
inlink->max_samples = nb_samples;
return 0;
}
static int config_output(AVFilterLink *outlink)
{
ShowVolumeContext *s = outlink->src->priv;
AVFilterLink *inlink = outlink->src->inputs[0];
outlink->w = s->w;
outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
outlink->sample_aspect_ratio = (AVRational){1,1};
outlink->frame_rate = s->frame_rate;
return 0;
}
static void drawtext(AVFrame *pic, int x, int y, const char *txt)
{
const uint8_t *font;
int font_height;
int i;
font = avpriv_cga_font, font_height = 8;
for (i = 0; txt[i]; i++) {
int char_y, mask;
uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
for (char_y = 0; char_y < font_height; char_y++) {
for (mask = 0x80; mask; mask >>= 1) {
if (font[txt[i] * font_height + char_y] & mask)
AV_WN32(p, ~AV_RN32(p));
p += 4;
}
p += pic->linesize[0] - 8*4;
}
}
}
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
{
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
ShowVolumeContext *s = ctx->priv;
int c, i, j, k;
double values[VAR_VARS_NB];
if (!s->out || s->out->width != outlink->w ||
s->out->height != outlink->h) {
av_frame_free(&s->out);
s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!s->out) {
av_frame_free(&insamples);
return AVERROR(ENOMEM);
}
for (i = 0; i < outlink->h; i++)
memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
}
s->out->pts = insamples->pts;
for (j = 0; j < outlink->h; j++) {
uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
for (k = 0; k < s->w; k++) {
dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] - s->f, 0);
dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] - s->f, 0);
dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] - s->f, 0);
dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] - s->f, 0);
}
}
for (c = 0; c < inlink->channels; c++) {
float *src = (float *)insamples->extended_data[c];
float max = 0;
int color;
for (i = 0; i < insamples->nb_samples; i++)
max = FFMAX(max, src[i]);
max = av_clipf(max, 0, 1);
values[VAR_VOLUME] = 20.0 * log(max) / M_LN10;
values[VAR_CHANNEL] = c;
color = av_expr_eval(s->c_expr, values, NULL);
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++)
AV_WN32A(dst + k * 4, color);
}
if (s->h >= 8 && s->draw_text)
drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2,
av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c)));
}
av_frame_free(&insamples);
return ff_filter_frame(outlink, av_frame_clone(s->out));
}
static av_cold void uninit(AVFilterContext *ctx)
{
ShowVolumeContext *s = ctx->priv;
av_frame_free(&s->out);
av_expr_free(s->c_expr);
}
static const AVFilterPad showvolume_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_input,
.filter_frame = filter_frame,
},
{ NULL }
};
static const AVFilterPad showvolume_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_output,
},
{ NULL }
};
AVFilter ff_avf_showvolume = {
.name = "showvolume",
.description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
.init = init,
.uninit = uninit,
.query_formats = query_formats,
.priv_size = sizeof(ShowVolumeContext),
.inputs = showvolume_inputs,
.outputs = showvolume_outputs,
.priv_class = &showvolume_class,
};
|