FFmpeg
Loading...
Searching...
No Matches
avf_showwaves.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Stefano Sabatini
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/**
22 * @file
23 * audio to video multimedia filter
24 */
25
26#include "config_components.h"
27
28#include "libavutil/avassert.h"
29#include "libavutil/avstring.h"
32#include "libavutil/mem.h"
33#include "libavutil/opt.h"
35#include "avfilter.h"
36#include "filters.h"
37#include "formats.h"
38#include "audio.h"
39#include "video.h"
40
48
56
62
68
69struct frame_node {
72};
73
74typedef struct ShowWavesContext {
75 const AVClass *class;
76 int w, h;
78 char *colors;
80 int16_t *buf_idy; /* y coordinate of previous sample for each channel */
81 int16_t *history;
87 int mode; ///< ShowWavesMode
88 int scale; ///< ShowWavesScale
89 int draw_mode; ///< ShowWavesDrawMode
92 uint8_t *fg;
93
94 int (*get_h)(int16_t sample, int height);
95 void (*draw_sample)(uint8_t *buf, int height, int linesize,
96 int16_t *prev_y, const uint8_t color[4], int h);
97
98 /* single picture */
103 int64_t *sum; /* abs sum of the samples per channel */
105
106#define OFFSET(x) offsetof(ShowWavesContext, x)
107#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
108
109static const AVOption showwaves_options[] = {
110 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
111 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
112 { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, .flags=FLAGS, .unit="mode"},
113 { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
114 { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
115 { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
116 { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
117 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_RATIONAL, {.i64 = 0}, 0, INT_MAX, FLAGS },
118 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
119 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
120 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
121 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
122 { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
123 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
124 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
125 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
126 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
127 { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
128 { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
129 { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
130 { NULL }
131};
132
134
136{
137 ShowWavesContext *showwaves = ctx->priv;
138
139 av_frame_free(&showwaves->outpicref);
140 av_freep(&showwaves->buf_idy);
141 av_freep(&showwaves->history);
142 av_freep(&showwaves->fg);
143
144 if (showwaves->single_pic) {
145 struct frame_node *node = showwaves->audio_frames;
146 while (node) {
147 struct frame_node *tmp = node;
148
149 node = node->next;
150 av_frame_free(&tmp->frame);
151 av_freep(&tmp);
152 }
153 av_freep(&showwaves->sum);
154 showwaves->last_frame = NULL;
155 }
156}
157
159 AVFilterFormatsConfig **cfg_in,
160 AVFilterFormatsConfig **cfg_out)
161{
165 int ret;
166
167 /* set input audio formats */
169 if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0)
170 return ret;
171
172 /* set output video format */
174 if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0)
175 return ret;
176
177 return 0;
178}
179
180static int get_lin_h(int16_t sample, int height)
181{
182 return height/2 - av_rescale(sample, height/2, INT16_MAX);
183}
184
185static int get_lin_h2(int16_t sample, int height)
186{
187 return av_rescale(FFABS(sample), height, INT16_MAX);
188}
189
190static int get_log_h(int16_t sample, int height)
191{
192 return height/2 - FFSIGN(sample) * (log10(1 + FFABS(sample)) * (height/2) / log10(1 + INT16_MAX));
193}
194
195static int get_log_h2(int16_t sample, int height)
196{
197 return log10(1 + FFABS(sample)) * height / log10(1 + INT16_MAX);
198}
199
200static int get_sqrt_h(int16_t sample, int height)
201{
202 return height/2 - FFSIGN(sample) * (sqrt(FFABS(sample)) * (height/2) / sqrt(INT16_MAX));
203}
204
205static int get_sqrt_h2(int16_t sample, int height)
206{
207 return sqrt(FFABS(sample)) * height / sqrt(INT16_MAX);
208}
209
210static int get_cbrt_h(int16_t sample, int height)
211{
212 return height/2 - FFSIGN(sample) * (cbrt(FFABS(sample)) * (height/2) / cbrt(INT16_MAX));
213}
214
215static int get_cbrt_h2(int16_t sample, int height)
216{
217 return cbrt(FFABS(sample)) * height / cbrt(INT16_MAX);
218}
219
220static void draw_sample_point_rgba_scale(uint8_t *buf, int height, int linesize,
221 int16_t *prev_y,
222 const uint8_t color[4], int h)
223{
224 if (h >= 0 && h < height) {
225 buf[h * linesize + 0] += color[0];
226 buf[h * linesize + 1] += color[1];
227 buf[h * linesize + 2] += color[2];
228 buf[h * linesize + 3] += color[3];
229 }
230}
231
232static void draw_sample_point_rgba_full(uint8_t *buf, int height, int linesize,
233 int16_t *prev_y,
234 const uint8_t color[4], int h)
235{
236 uint32_t clr = AV_RN32(color);
237 if (h >= 0 && h < height)
238 AV_WN32(buf + h * linesize, clr);
239}
240
241static void draw_sample_line_rgba_scale(uint8_t *buf, int height, int linesize,
242 int16_t *prev_y,
243 const uint8_t color[4], int h)
244{
245 int start = height/2;
246 int end = av_clip(h, 0, height-1);
247 uint8_t *bufk;
248 if (start > end)
249 FFSWAP(int16_t, start, end);
250 bufk = buf + start * linesize;
251 for (int k = start; k < end; k++, bufk += linesize) {
252 bufk[0] += color[0];
253 bufk[1] += color[1];
254 bufk[2] += color[2];
255 bufk[3] += color[3];
256 }
257}
258
259static void draw_sample_line_rgba_full(uint8_t *buf, int height, int linesize,
260 int16_t *prev_y,
261 const uint8_t color[4], int h)
262{
263 int start = height/2;
264 int end = av_clip(h, 0, height-1);
265 uint32_t clr = AV_RN32(color);
266 uint8_t *bufk;
267 if (start > end)
268 FFSWAP(int16_t, start, end);
269 bufk = buf + start * linesize;
270 for (int k = start; k < end; k++, bufk += linesize)
271 AV_WN32(bufk, clr);
272}
273
274static void draw_sample_p2p_rgba_scale(uint8_t *buf, int height, int linesize,
275 int16_t *prev_y,
276 const uint8_t color[4], int h)
277{
278 if (h >= 0 && h < height) {
279 buf[h * linesize + 0] += color[0];
280 buf[h * linesize + 1] += color[1];
281 buf[h * linesize + 2] += color[2];
282 buf[h * linesize + 3] += color[3];
283 if (*prev_y && h != *prev_y) {
284 int start = *prev_y;
285 uint8_t *bufk;
286 int end = av_clip(h, 0, height-1);
287 if (start > end)
288 FFSWAP(int16_t, start, end);
289 bufk = buf + (start + 1) * linesize;
290 for (int k = start + 1; k < end; k++, bufk += linesize) {
291 bufk[0] += color[0];
292 bufk[1] += color[1];
293 bufk[2] += color[2];
294 bufk[3] += color[3];
295 }
296 }
297 }
298 *prev_y = h;
299}
300
301static void draw_sample_p2p_rgba_full(uint8_t *buf, int height, int linesize,
302 int16_t *prev_y,
303 const uint8_t color[4], int h)
304{
305 uint32_t clr = AV_RN32(color);
306 if (h >= 0 && h < height) {
307 AV_WN32(buf + h * linesize, clr);
308 if (*prev_y && h != *prev_y) {
309 int start = *prev_y;
310 uint8_t *bufk;
311 int end = av_clip(h, 0, height-1);
312 if (start > end)
313 FFSWAP(int16_t, start, end);
314 bufk = buf + (start + 1) * linesize;
315 for (int k = start + 1; k < end; k++, bufk += linesize)
316 AV_WN32(bufk, clr);
317 }
318 }
319 *prev_y = h;
320}
321
322static void draw_sample_cline_rgba_scale(uint8_t *buf, int height, int linesize,
323 int16_t *prev_y,
324 const uint8_t color[4], int h)
325{
326 const int start = (height - h) / 2;
327 const int end = start + h;
328 uint8_t *bufk = buf + start * linesize;
329 for (int k = start; k < end; k++, bufk += linesize) {
330 bufk[0] += color[0];
331 bufk[1] += color[1];
332 bufk[2] += color[2];
333 bufk[3] += color[3];
334 }
335}
336
337static void draw_sample_cline_rgba_full(uint8_t *buf, int height, int linesize,
338 int16_t *prev_y,
339 const uint8_t color[4], int h)
340{
341 uint32_t clr = AV_RN32(color);
342 const int start = (height - h) / 2;
343 const int end = start + h;
344 uint8_t *bufk = buf + start * linesize;
345 for (int k = start; k < end; k++, bufk += linesize)
346 AV_WN32(bufk, clr);
347}
348
349static void draw_sample_point_gray(uint8_t *buf, int height, int linesize,
350 int16_t *prev_y,
351 const uint8_t color[4], int h)
352{
353 if (h >= 0 && h < height)
354 buf[h * linesize] += color[0];
355}
356
357static void draw_sample_line_gray(uint8_t *buf, int height, int linesize,
358 int16_t *prev_y,
359 const uint8_t color[4], int h)
360{
361 int k;
362 int start = height/2;
363 int end = av_clip(h, 0, height-1);
364 if (start > end)
365 FFSWAP(int16_t, start, end);
366 for (k = start; k < end; k++)
367 buf[k * linesize] += color[0];
368}
369
370static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize,
371 int16_t *prev_y,
372 const uint8_t color[4], int h)
373{
374 int k;
375 if (h >= 0 && h < height) {
376 buf[h * linesize] += color[0];
377 if (*prev_y && h != *prev_y) {
378 int start = *prev_y;
379 int end = av_clip(h, 0, height-1);
380 if (start > end)
381 FFSWAP(int16_t, start, end);
382 for (k = start + 1; k < end; k++)
383 buf[k * linesize] += color[0];
384 }
385 }
386 *prev_y = h;
387}
388
389static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize,
390 int16_t *prev_y,
391 const uint8_t color[4], int h)
392{
393 int k;
394 const int start = (height - h) / 2;
395 const int end = start + h;
396 for (k = start; k < end; k++)
397 buf[k * linesize] += color[0];
398}
399
400static int config_output(AVFilterLink *outlink)
401{
402 FilterLink *l = ff_filter_link(outlink);
403 AVFilterContext *ctx = outlink->src;
404 AVFilterLink *inlink = ctx->inputs[0];
405 ShowWavesContext *showwaves = ctx->priv;
406 int nb_channels = inlink->ch_layout.nb_channels;
407 char *colors, *saveptr = NULL;
408 uint8_t x;
409 int ch;
410
411 showwaves->q = av_make_q(0, 1);
412 showwaves->c = av_make_q(0, 1);
413
414 if (showwaves->single_pic) {
415 showwaves->n = av_make_q(1, 1);
416 l->frame_rate = av_make_q(1, 1);
417 } else {
418 if (!showwaves->n.num || !showwaves->n.den) {
419 showwaves->n = av_mul_q(av_make_q(inlink->sample_rate,
420 showwaves->w), av_inv_q(showwaves->rate));
421 l->frame_rate = showwaves->rate;
422 } else {
423 l->frame_rate = av_div_q(av_make_q(inlink->sample_rate, showwaves->w), showwaves->n);
424 }
425 }
426
427 showwaves->buf_idx = 0;
428 if (!FF_ALLOCZ_TYPED_ARRAY(showwaves->buf_idy, nb_channels)) {
429 av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
430 return AVERROR(ENOMEM);
431 }
432
433 showwaves->history_nb_samples = av_rescale(showwaves->w * nb_channels * 2,
434 showwaves->n.num, showwaves->n.den);
435 if (showwaves->history_nb_samples <= 0)
436 return AVERROR(EINVAL);
437 showwaves->history = av_calloc(showwaves->history_nb_samples,
438 sizeof(*showwaves->history));
439 if (!showwaves->history)
440 return AVERROR(ENOMEM);
441
442 outlink->time_base = av_inv_q(l->frame_rate);
443 outlink->w = showwaves->w;
444 outlink->h = showwaves->h;
445 outlink->sample_aspect_ratio = (AVRational){1,1};
446
447 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%f\n",
448 showwaves->w, showwaves->h, av_q2d(l->frame_rate), av_q2d(showwaves->n));
449
450 switch (outlink->format) {
451 case AV_PIX_FMT_GRAY8:
452 switch (showwaves->mode) {
453 case MODE_POINT: showwaves->draw_sample = draw_sample_point_gray; break;
454 case MODE_LINE: showwaves->draw_sample = draw_sample_line_gray; break;
455 case MODE_P2P: showwaves->draw_sample = draw_sample_p2p_gray; break;
456 case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline_gray; break;
457 default:
458 return AVERROR_BUG;
459 }
460 showwaves->pixstep = 1;
461 break;
462 case AV_PIX_FMT_RGBA:
463 switch (showwaves->mode) {
468 default:
469 return AVERROR_BUG;
470 }
471 showwaves->pixstep = 4;
472 break;
473 }
474
475 switch (showwaves->scale) {
476 case SCALE_LIN:
477 switch (showwaves->mode) {
478 case MODE_POINT:
479 case MODE_LINE:
480 case MODE_P2P: showwaves->get_h = get_lin_h; break;
481 case MODE_CENTERED_LINE: showwaves->get_h = get_lin_h2; break;
482 default:
483 return AVERROR_BUG;
484 }
485 break;
486 case SCALE_LOG:
487 switch (showwaves->mode) {
488 case MODE_POINT:
489 case MODE_LINE:
490 case MODE_P2P: showwaves->get_h = get_log_h; break;
491 case MODE_CENTERED_LINE: showwaves->get_h = get_log_h2; break;
492 default:
493 return AVERROR_BUG;
494 }
495 break;
496 case SCALE_SQRT:
497 switch (showwaves->mode) {
498 case MODE_POINT:
499 case MODE_LINE:
500 case MODE_P2P: showwaves->get_h = get_sqrt_h; break;
501 case MODE_CENTERED_LINE: showwaves->get_h = get_sqrt_h2; break;
502 default:
503 return AVERROR_BUG;
504 }
505 break;
506 case SCALE_CBRT:
507 switch (showwaves->mode) {
508 case MODE_POINT:
509 case MODE_LINE:
510 case MODE_P2P: showwaves->get_h = get_cbrt_h; break;
511 case MODE_CENTERED_LINE: showwaves->get_h = get_cbrt_h2; break;
512 default:
513 return AVERROR_BUG;
514 }
515 break;
516 }
517
518 showwaves->fg = av_malloc_array(nb_channels, 4 * sizeof(*showwaves->fg));
519 if (!showwaves->fg)
520 return AVERROR(ENOMEM);
521
522 colors = av_strdup(showwaves->colors);
523 if (!colors)
524 return AVERROR(ENOMEM);
525
526 if (showwaves->draw_mode == DRAW_SCALE) {
527 /* multiplication factor, pre-computed to avoid in-loop divisions */
528 x = (showwaves->n.den * 255) / ((showwaves->split_channels ? 1 : nb_channels) * showwaves->n.num);
529 } else {
530 x = 255;
531 }
532 if (outlink->format == AV_PIX_FMT_RGBA) {
533 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
534
535 for (ch = 0; ch < nb_channels; ch++) {
536 char *color;
537
538 color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
539 if (color)
540 av_parse_color(fg, color, -1, ctx);
541 showwaves->fg[4*ch + 0] = fg[0] * x / 255.;
542 showwaves->fg[4*ch + 1] = fg[1] * x / 255.;
543 showwaves->fg[4*ch + 2] = fg[2] * x / 255.;
544 showwaves->fg[4*ch + 3] = fg[3] * x / 255.;
545 }
546 } else {
547 for (ch = 0; ch < nb_channels; ch++)
548 showwaves->fg[4 * ch + 0] = x;
549 }
550 av_free(colors);
551
552 return 0;
553}
554
555inline static int push_frame(AVFilterLink *outlink, int i, int64_t pts)
556{
557 AVFilterContext *ctx = outlink->src;
558 AVFilterLink *inlink = ctx->inputs[0];
559 ShowWavesContext *showwaves = outlink->src->priv;
560 int nb_channels = inlink->ch_layout.nb_channels;
561 int ret;
562
563 showwaves->outpicref->duration = 1;
564 showwaves->outpicref->pts = av_rescale_q(pts + i,
565 inlink->time_base,
566 outlink->time_base);
567
568 ret = ff_filter_frame(outlink, showwaves->outpicref);
569 showwaves->outpicref = NULL;
570 showwaves->buf_idx = 0;
571 for (int i = 0; i < nb_channels; i++)
572 showwaves->buf_idy[i] = 0;
573 return ret;
574}
575
576static int push_single_pic(AVFilterLink *outlink)
577{
578 AVFilterContext *ctx = outlink->src;
579 AVFilterLink *inlink = ctx->inputs[0];
580 ShowWavesContext *showwaves = ctx->priv;
581 int64_t n = 0, column_max_samples = showwaves->total_samples / outlink->w;
582 int64_t remaining_samples = showwaves->total_samples - (column_max_samples * outlink->w);
583 int64_t last_column_samples = column_max_samples + remaining_samples;
584 AVFrame *out = showwaves->outpicref;
585 struct frame_node *node;
586 const int nb_channels = inlink->ch_layout.nb_channels;
587 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
588 const int linesize = out->linesize[0];
589 const int pixstep = showwaves->pixstep;
590 int col = 0;
591 int64_t *sum = showwaves->sum;
592
593 if (column_max_samples == 0) {
594 av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
595 return AVERROR(EINVAL);
596 }
597
598 av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", column_max_samples);
599
600 memset(sum, 0, nb_channels * sizeof(*sum));
601
602 for (node = showwaves->audio_frames; node; node = node->next) {
603 int i;
604 const AVFrame *frame = node->frame;
605 const int16_t *p = (const int16_t *)frame->data[0];
606
607 for (i = 0; i < frame->nb_samples; i++) {
608 int64_t max_samples = col == outlink->w - 1 ? last_column_samples: column_max_samples;
609 int ch;
610
611 switch (showwaves->filter_mode) {
612 case FILTER_AVERAGE:
613 for (ch = 0; ch < nb_channels; ch++)
614 sum[ch] += abs(p[ch + i*nb_channels]);
615 break;
616 case FILTER_PEAK:
617 for (ch = 0; ch < nb_channels; ch++)
618 sum[ch] = FFMAX(sum[ch], abs(p[ch + i*nb_channels]));
619 break;
620 }
621
622 n++;
623 if (n == max_samples) {
624 for (ch = 0; ch < nb_channels; ch++) {
625 int16_t sample = sum[ch] / (showwaves->filter_mode == FILTER_AVERAGE ? max_samples : 1);
626 uint8_t *buf = out->data[0] + col * pixstep;
627 int h;
628
629 if (showwaves->split_channels)
630 buf += ch*ch_height*linesize;
631 av_assert0(col < outlink->w);
632 h = showwaves->get_h(sample, ch_height);
633 showwaves->draw_sample(buf, ch_height, linesize, &showwaves->buf_idy[ch], &showwaves->fg[ch * 4], h);
634 sum[ch] = 0;
635 }
636 col++;
637 n = 0;
638 }
639 }
640 }
641
642 return push_frame(outlink, 0, 0);
643}
644
645
646static int request_frame(AVFilterLink *outlink)
647{
648 ShowWavesContext *showwaves = outlink->src->priv;
649 AVFilterLink *inlink = outlink->src->inputs[0];
650 int ret;
651
652 ret = ff_request_frame(inlink);
653 if (ret == AVERROR_EOF && showwaves->outpicref) {
654 push_single_pic(outlink);
655 }
656
657 return ret;
658}
659
660static int alloc_out_frame(ShowWavesContext *showwaves,
661 AVFilterLink *outlink)
662{
663 if (!showwaves->outpicref) {
664 AVFrame *out = showwaves->outpicref =
665 ff_get_video_buffer(outlink, outlink->w, outlink->h);
666 if (!out)
667 return AVERROR(ENOMEM);
668 out->width = outlink->w;
669 out->height = outlink->h;
670 for (int j = 0; j < outlink->h; j++)
671 memset(out->data[0] + j*out->linesize[0], 0, outlink->w * showwaves->pixstep);
672 }
673 return 0;
674}
675
677{
678 ShowWavesContext *showwaves = ctx->priv;
679
680 if (!strcmp(ctx->filter->name, "showwavespic")) {
681 showwaves->single_pic = 1;
682 showwaves->mode = MODE_CENTERED_LINE;
683 }
684
685 return 0;
686}
687
688#if CONFIG_SHOWWAVES_FILTER
689
690static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
691{
692 AVFilterContext *ctx = inlink->dst;
693 AVFilterLink *outlink = ctx->outputs[0];
694 ShowWavesContext *showwaves = ctx->priv;
695 const int nb_samples = insamples->nb_samples;
696 AVFrame *outpicref = showwaves->outpicref;
697 const int16_t *p = (const int16_t *)insamples->data[0];
698 int16_t *history = showwaves->history;
699 const int nb_channels = inlink->ch_layout.nb_channels;
700 int i, j, ret = 0, linesize;
701 const int pixstep = showwaves->pixstep;
702 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
703 const int history_nb_samples = showwaves->history_nb_samples;
704 const int split_channels = showwaves->split_channels;
705 const AVRational i_n = av_inv_q(showwaves->n);
706 const AVRational u_q = av_make_q(1, 1);
707 const AVRational z_q = av_make_q(0, 1);
708 int16_t *buf_idy = showwaves->buf_idy;
709 int idx = showwaves->history_index;
710 int buf_idx = showwaves->buf_idx;
711 const uint8_t *fg = showwaves->fg;
712 const int w = showwaves->w;
713 uint8_t *dst;
714
715 for (int n = 0; n < nb_samples * nb_channels; n++) {
716 history[idx++] = p[n];
717 if (idx >= history_nb_samples)
718 idx = 0;
719 }
720 showwaves->history_index = idx;
721
722 ret = alloc_out_frame(showwaves, outlink);
723 if (ret < 0)
724 goto end;
725 outpicref = showwaves->outpicref;
726 linesize = outpicref->linesize[0];
727
728 /* draw data in the buffer */
729 dst = outpicref->data[0];
730 for (i = 0; i < history_nb_samples; i++) {
731 for (j = 0; j < nb_channels; j++) {
732 uint8_t *buf = dst + buf_idx * pixstep;
733 int h;
734
735 if (split_channels)
736 buf += j*ch_height*linesize;
737 h = showwaves->get_h(history[idx++], ch_height);
738 if (idx >= history_nb_samples)
739 idx = 0;
740 showwaves->draw_sample(buf, ch_height, linesize,
741 &buf_idy[j], &fg[j * 4], h);
742 }
743
744 showwaves->c = av_add_q(showwaves->c, i_n);
745 if (av_cmp_q(showwaves->c, u_q) >= 0) {
746 showwaves->c = z_q;
747 buf_idx++;
748 }
749 if (buf_idx == w)
750 break;
751 }
752
753 showwaves->buf_idx = buf_idx;
754
755 if ((ret = push_frame(outlink, history_nb_samples - i - 1, insamples->pts)) < 0)
756 goto end;
757 outpicref = showwaves->outpicref;
758end:
759 av_frame_free(&insamples);
760 return ret;
761}
762
763static int activate(AVFilterContext *ctx)
764{
765 AVFilterLink *inlink = ctx->inputs[0];
766 AVFilterLink *outlink = ctx->outputs[0];
767 ShowWavesContext *showwaves = ctx->priv;
768 AVRational q;
769 AVFrame *in;
770 int nb_samples;
771 int ret;
772
773 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
774
775 q = av_add_q(showwaves->q, av_mul_q(av_make_q(outlink->w, 1), showwaves->n));
776 nb_samples = (q.num + (q.den / 2)) / q.den;
777 ret = ff_inlink_consume_samples(inlink, nb_samples, nb_samples, &in);
778 if (ret < 0)
779 return ret;
780 if (ret > 0) {
781 showwaves->q = av_sub_q(q, av_make_q(nb_samples, 1));
782 return showwaves_filter_frame(inlink, in);
783 }
784
785 FF_FILTER_FORWARD_STATUS(inlink, outlink);
786 FF_FILTER_FORWARD_WANTED(outlink, inlink);
787
788 return FFERROR_NOT_READY;
789}
790
791static const AVFilterPad showwaves_outputs[] = {
792 {
793 .name = "default",
794 .type = AVMEDIA_TYPE_VIDEO,
795 .config_props = config_output,
796 },
797};
798
800 .p.name = "showwaves",
801 .p.description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
802 .p.priv_class = &showwaves_class,
803 .init = init,
804 .uninit = uninit,
805 .priv_size = sizeof(ShowWavesContext),
807 .activate = activate,
808 FILTER_OUTPUTS(showwaves_outputs),
810};
811
812#endif // CONFIG_SHOWWAVES_FILTER
813
814#if CONFIG_SHOWWAVESPIC_FILTER
815
816#define OFFSET(x) offsetof(ShowWavesContext, x)
817#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
818
819static const AVOption showwavespic_options[] = {
820 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
821 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
822 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
823 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
824 { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
825 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
826 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
827 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
828 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
829 { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
830 { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
831 { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
832 { "filter", "set filter mode", OFFSET(filter_mode), AV_OPT_TYPE_INT, {.i64 = FILTER_AVERAGE}, 0, FILTER_NB-1, FLAGS, .unit="filter" },
833 { "average", "use average samples", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_AVERAGE}, .flags=FLAGS, .unit="filter"},
834 { "peak", "use peak samples", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_PEAK}, .flags=FLAGS, .unit="filter"},
835 { NULL }
836};
837
838AVFILTER_DEFINE_CLASS(showwavespic);
839
840static int showwavespic_config_input(AVFilterLink *inlink)
841{
842 AVFilterContext *ctx = inlink->dst;
843 ShowWavesContext *showwaves = ctx->priv;
844
845 if (showwaves->single_pic) {
846 showwaves->sum = av_calloc(inlink->ch_layout.nb_channels, sizeof(*showwaves->sum));
847 if (!showwaves->sum)
848 return AVERROR(ENOMEM);
849 }
850
851 return 0;
852}
853
854static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
855{
856 AVFilterContext *ctx = inlink->dst;
857 AVFilterLink *outlink = ctx->outputs[0];
858 ShowWavesContext *showwaves = ctx->priv;
859 int ret = 0;
860
861 if (showwaves->single_pic) {
862 struct frame_node *f;
863
864 ret = alloc_out_frame(showwaves, outlink);
865 if (ret < 0)
866 goto end;
867
868 /* queue the audio frame */
869 f = av_malloc(sizeof(*f));
870 if (!f) {
871 ret = AVERROR(ENOMEM);
872 goto end;
873 }
874 f->frame = insamples;
875 f->next = NULL;
876 if (!showwaves->last_frame) {
877 showwaves->audio_frames =
878 showwaves->last_frame = f;
879 } else {
880 showwaves->last_frame->next = f;
881 showwaves->last_frame = f;
882 }
883 showwaves->total_samples += insamples->nb_samples;
884
885 return 0;
886 }
887
888end:
889 av_frame_free(&insamples);
890 return ret;
891}
892
893static const AVFilterPad showwavespic_inputs[] = {
894 {
895 .name = "default",
896 .type = AVMEDIA_TYPE_AUDIO,
897 .config_props = showwavespic_config_input,
898 .filter_frame = showwavespic_filter_frame,
899 },
900};
901
902static const AVFilterPad showwavespic_outputs[] = {
903 {
904 .name = "default",
905 .type = AVMEDIA_TYPE_VIDEO,
906 .config_props = config_output,
907 .request_frame = request_frame,
908 },
909};
910
912 .p.name = "showwavespic",
913 .p.description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
914 .p.priv_class = &showwavespic_class,
915 .init = init,
916 .uninit = uninit,
917 .priv_size = sizeof(ShowWavesContext),
918 FILTER_INPUTS(showwavespic_inputs),
919 FILTER_OUTPUTS(showwavespic_outputs),
921};
922
923#endif // CONFIG_SHOWWAVESPIC_FILTER
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
const FFFilter ff_avf_showwavespic
const FFFilter ff_avf_showwaves
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
@ SCALE_NB
@ MODE_NB
@ DRAW_NB
static int get_sqrt_h(int16_t sample, int height)
ShowWavesFilterMode
@ FILTER_NB
@ FILTER_AVERAGE
@ FILTER_PEAK
ShowWavesMode
@ MODE_CENTERED_LINE
@ MODE_LINE
@ MODE_POINT
@ MODE_P2P
static int get_lin_h2(int16_t sample, int height)
static int push_single_pic(AVFilterLink *outlink)
static void draw_sample_point_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int get_sqrt_h2(int16_t sample, int height)
static void draw_sample_line_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static void draw_sample_cline_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int get_cbrt_h2(int16_t sample, int height)
static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int request_frame(AVFilterLink *outlink)
static void draw_sample_line_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static void draw_sample_p2p_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int get_lin_h(int16_t sample, int height)
static void draw_sample_point_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static void draw_sample_line_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static const AVOption showwaves_options[]
static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static void draw_sample_cline_rgba_full(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static av_cold void uninit(AVFilterContext *ctx)
static int alloc_out_frame(ShowWavesContext *showwaves, AVFilterLink *outlink)
static int get_log_h(int16_t sample, int height)
ShowWavesScale
@ SCALE_LOG
@ SCALE_CBRT
@ SCALE_SQRT
@ SCALE_LIN
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
static int get_cbrt_h(int16_t sample, int height)
static int get_log_h2(int16_t sample, int height)
static void draw_sample_point_gray(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
static int push_frame(AVFilterLink *outlink, int i, int64_t pts)
static void draw_sample_p2p_rgba_scale(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
ShowWavesDrawMode
@ DRAW_FULL
@ DRAW_SCALE
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1540
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define av_clip
Definition common.h:100
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define FFSIGN(a)
Definition common.h:75
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define abs(x)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define sample
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
av_warn_unused_result AVFilterFormats * ff_make_sample_format_list(const enum AVSampleFormat *fmts)
Create a list of supported sample formats.
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition opt.h:279
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition rational.c:93
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition rational.c:101
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition rational.c:88
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_WN32(p, v)
#define AV_RN32(p)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition internal.h:72
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition parseutils.c:359
misc parsing utilities
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
formats
Definition signature.h:47
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:820
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int draw_mode
ShowWavesDrawMode.
int mode
ShowWavesMode.
void(* draw_sample)(uint8_t *buf, int height, int linesize, int16_t *prev_y, const uint8_t color[4], int h)
struct frame_node * last_frame
int(* get_h)(int16_t sample, int height)
struct frame_node * audio_frames
int scale
ShowWavesScale.
AVFrame * outpicref
AVFrame * frame
struct frame_node * next
Definition swscale.c:71
#define cbrt
Definition tablegen.h:35
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
static int64_t pts
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89