FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avf_showvolume.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 233 0.0%
Functions: 0 14 0.0%
Branches: 0 161 0.0%

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