FFmpeg coverage


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