2 * Copyright (c) 2012 Stefano Sabatini
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
23 * audio to video multimedia filter
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
54 enum ShowWavesDrawMode {
62 struct frame_node *next;
65 typedef struct ShowWavesContext {
71 int16_t *buf_idy; /* y coordinate of previous sample for each channel */
76 int mode; ///< ShowWavesMode
77 int scale; ///< ShowWavesScale
78 int draw_mode; ///< ShowWavesDrawMode
82 int (*get_h)(int16_t sample, int height);
83 void (*draw_sample)(uint8_t *buf, int height, int linesize,
84 int16_t *prev_y, const uint8_t color[4], int h);
88 struct frame_node *audio_frames;
89 struct frame_node *last_frame;
90 int64_t total_samples;
91 int64_t *sum; /* abs sum of the samples per channel */
94 #define OFFSET(x) offsetof(ShowWavesContext, x)
95 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
97 static const AVOption showwaves_options[] = {
98 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
99 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
100 { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
101 { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
102 { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
103 { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
104 { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
105 { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
106 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
107 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
108 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
109 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
110 { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
111 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
112 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
113 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
114 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
115 { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
116 { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
117 { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
121 AVFILTER_DEFINE_CLASS(showwaves);
123 static av_cold void uninit(AVFilterContext *ctx)
125 ShowWavesContext *showwaves = ctx->priv;
127 av_frame_free(&showwaves->outpicref);
128 av_freep(&showwaves->buf_idy);
129 av_freep(&showwaves->fg);
131 if (showwaves->single_pic) {
132 struct frame_node *node = showwaves->audio_frames;
134 struct frame_node *tmp = node;
137 av_frame_free(&tmp->frame);
140 av_freep(&showwaves->sum);
141 showwaves->last_frame = NULL;
145 static int query_formats(AVFilterContext *ctx)
147 AVFilterFormats *formats = NULL;
148 AVFilterChannelLayouts *layouts = NULL;
149 AVFilterLink *inlink = ctx->inputs[0];
150 AVFilterLink *outlink = ctx->outputs[0];
151 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
152 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
155 /* set input audio formats */
156 formats = ff_make_format_list(sample_fmts);
157 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
160 layouts = ff_all_channel_layouts();
161 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
164 formats = ff_all_samplerates();
165 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
168 /* set output video format */
169 formats = ff_make_format_list(pix_fmts);
170 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
176 static int get_lin_h(int16_t sample, int height)
178 return height/2 - av_rescale(sample, height/2, INT16_MAX);
181 static int get_lin_h2(int16_t sample, int height)
183 return av_rescale(FFABS(sample), height, INT16_MAX);
186 static int get_log_h(int16_t sample, int height)
188 return height/2 - FFSIGN(sample) * (log10(1 + FFABS(sample)) * (height/2) / log10(1 + INT16_MAX));
191 static int get_log_h2(int16_t sample, int height)
193 return log10(1 + FFABS(sample)) * height / log10(1 + INT16_MAX);
196 static int get_sqrt_h(int16_t sample, int height)
198 return height/2 - FFSIGN(sample) * (sqrt(FFABS(sample)) * (height/2) / sqrt(INT16_MAX));
201 static int get_sqrt_h2(int16_t sample, int height)
203 return sqrt(FFABS(sample)) * height / sqrt(INT16_MAX);
206 static int get_cbrt_h(int16_t sample, int height)
208 return height/2 - FFSIGN(sample) * (cbrt(FFABS(sample)) * (height/2) / cbrt(INT16_MAX));
211 static int get_cbrt_h2(int16_t sample, int height)
213 return cbrt(FFABS(sample)) * height / cbrt(INT16_MAX);
216 static void draw_sample_point_rgba_scale(uint8_t *buf, int height, int linesize,
218 const uint8_t color[4], int h)
220 if (h >= 0 && h < height) {
221 buf[h * linesize + 0] += color[0];
222 buf[h * linesize + 1] += color[1];
223 buf[h * linesize + 2] += color[2];
224 buf[h * linesize + 3] += color[3];
228 static void draw_sample_point_rgba_full(uint8_t *buf, int height, int linesize,
230 const uint8_t color[4], int h)
232 if (h >= 0 && h < height) {
233 buf[h * linesize + 0] = color[0];
234 buf[h * linesize + 1] = color[1];
235 buf[h * linesize + 2] = color[2];
236 buf[h * linesize + 3] = color[3];
240 static void draw_sample_line_rgba_scale(uint8_t *buf, int height, int linesize,
242 const uint8_t color[4], int h)
245 int start = height/2;
246 int end = av_clip(h, 0, height-1);
248 FFSWAP(int16_t, start, end);
249 for (k = start; k < end; k++) {
250 buf[k * linesize + 0] += color[0];
251 buf[k * linesize + 1] += color[1];
252 buf[k * linesize + 2] += color[2];
253 buf[k * linesize + 3] += color[3];
257 static void draw_sample_line_rgba_full(uint8_t *buf, int height, int linesize,
259 const uint8_t color[4], int h)
262 int start = height/2;
263 int end = av_clip(h, 0, height-1);
265 FFSWAP(int16_t, start, end);
266 for (k = start; k < end; k++) {
267 buf[k * linesize + 0] = color[0];
268 buf[k * linesize + 1] = color[1];
269 buf[k * linesize + 2] = color[2];
270 buf[k * linesize + 3] = color[3];
274 static void draw_sample_p2p_rgba_scale(uint8_t *buf, int height, int linesize,
276 const uint8_t color[4], int h)
279 if (h >= 0 && h < height) {
280 buf[h * linesize + 0] += color[0];
281 buf[h * linesize + 1] += color[1];
282 buf[h * linesize + 2] += color[2];
283 buf[h * linesize + 3] += color[3];
284 if (*prev_y && h != *prev_y) {
286 int end = av_clip(h, 0, height-1);
288 FFSWAP(int16_t, start, end);
289 for (k = start + 1; k < end; k++) {
290 buf[k * linesize + 0] += color[0];
291 buf[k * linesize + 1] += color[1];
292 buf[k * linesize + 2] += color[2];
293 buf[k * linesize + 3] += color[3];
300 static void draw_sample_p2p_rgba_full(uint8_t *buf, int height, int linesize,
302 const uint8_t color[4], int h)
305 if (h >= 0 && h < height) {
306 buf[h * linesize + 0] = color[0];
307 buf[h * linesize + 1] = color[1];
308 buf[h * linesize + 2] = color[2];
309 buf[h * linesize + 3] = color[3];
310 if (*prev_y && h != *prev_y) {
312 int end = av_clip(h, 0, height-1);
314 FFSWAP(int16_t, start, end);
315 for (k = start + 1; k < end; k++) {
316 buf[k * linesize + 0] = color[0];
317 buf[k * linesize + 1] = color[1];
318 buf[k * linesize + 2] = color[2];
319 buf[k * linesize + 3] = color[3];
326 static void draw_sample_cline_rgba_scale(uint8_t *buf, int height, int linesize,
328 const uint8_t color[4], int h)
331 const int start = (height - h) / 2;
332 const int end = start + h;
333 for (k = start; k < end; k++) {
334 buf[k * linesize + 0] += color[0];
335 buf[k * linesize + 1] += color[1];
336 buf[k * linesize + 2] += color[2];
337 buf[k * linesize + 3] += color[3];
340 static void draw_sample_cline_rgba_full(uint8_t *buf, int height, int linesize,
342 const uint8_t color[4], int h)
345 const int start = (height - h) / 2;
346 const int end = start + h;
347 for (k = start; k < end; k++) {
348 buf[k * linesize + 0] = color[0];
349 buf[k * linesize + 1] = color[1];
350 buf[k * linesize + 2] = color[2];
351 buf[k * linesize + 3] = color[3];
355 static void draw_sample_point_gray(uint8_t *buf, int height, int linesize,
357 const uint8_t color[4], int h)
359 if (h >= 0 && h < height)
360 buf[h * linesize] += color[0];
363 static void draw_sample_line_gray(uint8_t *buf, int height, int linesize,
365 const uint8_t color[4], int h)
368 int start = height/2;
369 int end = av_clip(h, 0, height-1);
371 FFSWAP(int16_t, start, end);
372 for (k = start; k < end; k++)
373 buf[k * linesize] += color[0];
376 static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize,
378 const uint8_t color[4], int h)
381 if (h >= 0 && h < height) {
382 buf[h * linesize] += color[0];
383 if (*prev_y && h != *prev_y) {
385 int end = av_clip(h, 0, height-1);
387 FFSWAP(int16_t, start, end);
388 for (k = start + 1; k < end; k++)
389 buf[k * linesize] += color[0];
395 static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize,
397 const uint8_t color[4], int h)
400 const int start = (height - h) / 2;
401 const int end = start + h;
402 for (k = start; k < end; k++)
403 buf[k * linesize] += color[0];
406 static int config_output(AVFilterLink *outlink)
408 AVFilterContext *ctx = outlink->src;
409 AVFilterLink *inlink = ctx->inputs[0];
410 ShowWavesContext *showwaves = ctx->priv;
411 int nb_channels = inlink->channels;
412 char *colors, *saveptr = NULL;
416 if (showwaves->single_pic)
420 showwaves->n = FFMAX(1, av_rescale_q(inlink->sample_rate, av_make_q(1, showwaves->w), showwaves->rate));
422 showwaves->buf_idx = 0;
423 if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
424 av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
425 return AVERROR(ENOMEM);
427 outlink->w = showwaves->w;
428 outlink->h = showwaves->h;
429 outlink->sample_aspect_ratio = (AVRational){1,1};
431 outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
432 (AVRational){showwaves->w,1});
434 av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
435 showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
437 switch (outlink->format) {
438 case AV_PIX_FMT_GRAY8:
439 switch (showwaves->mode) {
440 case MODE_POINT: showwaves->draw_sample = draw_sample_point_gray; break;
441 case MODE_LINE: showwaves->draw_sample = draw_sample_line_gray; break;
442 case MODE_P2P: showwaves->draw_sample = draw_sample_p2p_gray; break;
443 case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline_gray; break;
447 showwaves->pixstep = 1;
449 case AV_PIX_FMT_RGBA:
450 switch (showwaves->mode) {
451 case MODE_POINT: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_point_rgba_scale : draw_sample_point_rgba_full; break;
452 case MODE_LINE: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_line_rgba_scale : draw_sample_line_rgba_full; break;
453 case MODE_P2P: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_p2p_rgba_scale : draw_sample_p2p_rgba_full; break;
454 case MODE_CENTERED_LINE: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_cline_rgba_scale : draw_sample_cline_rgba_full; break;
458 showwaves->pixstep = 4;
462 switch (showwaves->scale) {
464 switch (showwaves->mode) {
467 case MODE_P2P: showwaves->get_h = get_lin_h; break;
468 case MODE_CENTERED_LINE: showwaves->get_h = get_lin_h2; break;
474 switch (showwaves->mode) {
477 case MODE_P2P: showwaves->get_h = get_log_h; break;
478 case MODE_CENTERED_LINE: showwaves->get_h = get_log_h2; break;
484 switch (showwaves->mode) {
487 case MODE_P2P: showwaves->get_h = get_sqrt_h; break;
488 case MODE_CENTERED_LINE: showwaves->get_h = get_sqrt_h2; break;
494 switch (showwaves->mode) {
497 case MODE_P2P: showwaves->get_h = get_cbrt_h; break;
498 case MODE_CENTERED_LINE: showwaves->get_h = get_cbrt_h2; break;
505 showwaves->fg = av_malloc_array(nb_channels, 4 * sizeof(*showwaves->fg));
507 return AVERROR(ENOMEM);
509 colors = av_strdup(showwaves->colors);
511 return AVERROR(ENOMEM);
513 if (showwaves->draw_mode == DRAW_SCALE) {
514 /* multiplication factor, pre-computed to avoid in-loop divisions */
515 x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * showwaves->n);
519 if (outlink->format == AV_PIX_FMT_RGBA) {
520 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
522 for (ch = 0; ch < nb_channels; ch++) {
525 color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
527 av_parse_color(fg, color, -1, ctx);
528 showwaves->fg[4*ch + 0] = fg[0] * x / 255.;
529 showwaves->fg[4*ch + 1] = fg[1] * x / 255.;
530 showwaves->fg[4*ch + 2] = fg[2] * x / 255.;
531 showwaves->fg[4*ch + 3] = fg[3] * x / 255.;
534 for (ch = 0; ch < nb_channels; ch++)
535 showwaves->fg[4 * ch + 0] = x;
542 inline static int push_frame(AVFilterLink *outlink)
544 AVFilterContext *ctx = outlink->src;
545 AVFilterLink *inlink = ctx->inputs[0];
546 ShowWavesContext *showwaves = outlink->src->priv;
547 int nb_channels = inlink->channels;
550 ret = ff_filter_frame(outlink, showwaves->outpicref);
551 showwaves->outpicref = NULL;
552 showwaves->buf_idx = 0;
553 for (i = 0; i < nb_channels; i++)
554 showwaves->buf_idy[i] = 0;
558 static int push_single_pic(AVFilterLink *outlink)
560 AVFilterContext *ctx = outlink->src;
561 AVFilterLink *inlink = ctx->inputs[0];
562 ShowWavesContext *showwaves = ctx->priv;
563 int64_t n = 0, column_max_samples = showwaves->total_samples / outlink->w;
564 int64_t remaining_samples = showwaves->total_samples - (column_max_samples * outlink->w);
565 int64_t last_column_samples = column_max_samples + remaining_samples;
566 AVFrame *out = showwaves->outpicref;
567 struct frame_node *node;
568 const int nb_channels = inlink->channels;
569 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
570 const int linesize = out->linesize[0];
571 const int pixstep = showwaves->pixstep;
573 int64_t *sum = showwaves->sum;
575 if (column_max_samples == 0) {
576 av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
577 return AVERROR(EINVAL);
580 av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", column_max_samples);
582 memset(sum, 0, nb_channels);
584 for (node = showwaves->audio_frames; node; node = node->next) {
586 const AVFrame *frame = node->frame;
587 const int16_t *p = (const int16_t *)frame->data[0];
589 for (i = 0; i < frame->nb_samples; i++) {
590 int64_t max_samples = col == outlink->w - 1 ? last_column_samples: column_max_samples;
593 for (ch = 0; ch < nb_channels; ch++)
594 sum[ch] += abs(p[ch + i*nb_channels]) << 1;
596 if (n == max_samples) {
597 for (ch = 0; ch < nb_channels; ch++) {
598 int16_t sample = sum[ch] / max_samples;
599 uint8_t *buf = out->data[0] + col * pixstep;
602 if (showwaves->split_channels)
603 buf += ch*ch_height*linesize;
604 av_assert0(col < outlink->w);
605 h = showwaves->get_h(sample, ch_height);
606 showwaves->draw_sample(buf, ch_height, linesize, &showwaves->buf_idy[ch], &showwaves->fg[ch * 4], h);
615 return push_frame(outlink);
619 static int request_frame(AVFilterLink *outlink)
621 ShowWavesContext *showwaves = outlink->src->priv;
622 AVFilterLink *inlink = outlink->src->inputs[0];
625 ret = ff_request_frame(inlink);
626 if (ret == AVERROR_EOF && showwaves->outpicref) {
627 if (showwaves->single_pic)
628 push_single_pic(outlink);
636 static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
637 const AVFilterLink *inlink, AVFilterLink *outlink,
640 if (!showwaves->outpicref) {
642 AVFrame *out = showwaves->outpicref =
643 ff_get_video_buffer(outlink, outlink->w, outlink->h);
645 return AVERROR(ENOMEM);
646 out->width = outlink->w;
647 out->height = outlink->h;
648 out->pts = in->pts + av_rescale_q((p - (int16_t *)in->data[0]) / inlink->channels,
649 av_make_q(1, inlink->sample_rate),
651 for (j = 0; j < outlink->h; j++)
652 memset(out->data[0] + j*out->linesize[0], 0, outlink->w * showwaves->pixstep);
657 static av_cold int init(AVFilterContext *ctx)
659 ShowWavesContext *showwaves = ctx->priv;
661 if (!strcmp(ctx->filter->name, "showwavespic")) {
662 showwaves->single_pic = 1;
663 showwaves->mode = MODE_CENTERED_LINE;
669 #if CONFIG_SHOWWAVES_FILTER
671 static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
673 AVFilterContext *ctx = inlink->dst;
674 AVFilterLink *outlink = ctx->outputs[0];
675 ShowWavesContext *showwaves = ctx->priv;
676 const int nb_samples = insamples->nb_samples;
677 AVFrame *outpicref = showwaves->outpicref;
678 int16_t *p = (int16_t *)insamples->data[0];
679 int nb_channels = inlink->channels;
681 const int pixstep = showwaves->pixstep;
682 const int n = showwaves->n;
683 const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
685 /* draw data in the buffer */
686 for (i = 0; i < nb_samples; i++) {
688 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
691 outpicref = showwaves->outpicref;
693 for (j = 0; j < nb_channels; j++) {
694 uint8_t *buf = outpicref->data[0] + showwaves->buf_idx * pixstep;
695 const int linesize = outpicref->linesize[0];
698 if (showwaves->split_channels)
699 buf += j*ch_height*linesize;
700 h = showwaves->get_h(*p++, ch_height);
701 showwaves->draw_sample(buf, ch_height, linesize,
702 &showwaves->buf_idy[j], &showwaves->fg[j * 4], h);
705 showwaves->sample_count_mod++;
706 if (showwaves->sample_count_mod == n) {
707 showwaves->sample_count_mod = 0;
708 showwaves->buf_idx++;
710 if (showwaves->buf_idx == showwaves->w ||
711 (ff_outlink_get_status(inlink) && i == nb_samples - 1))
712 if ((ret = push_frame(outlink)) < 0)
714 outpicref = showwaves->outpicref;
718 av_frame_free(&insamples);
722 static int activate(AVFilterContext *ctx)
724 AVFilterLink *inlink = ctx->inputs[0];
725 AVFilterLink *outlink = ctx->outputs[0];
726 ShowWavesContext *showwaves = ctx->priv;
728 const int nb_samples = showwaves->n * outlink->w;
731 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
733 ret = ff_inlink_consume_samples(inlink, nb_samples, nb_samples, &in);
737 return showwaves_filter_frame(inlink, in);
739 FF_FILTER_FORWARD_STATUS(inlink, outlink);
740 FF_FILTER_FORWARD_WANTED(outlink, inlink);
742 return FFERROR_NOT_READY;
745 static const AVFilterPad showwaves_inputs[] = {
748 .type = AVMEDIA_TYPE_AUDIO,
753 static const AVFilterPad showwaves_outputs[] = {
756 .type = AVMEDIA_TYPE_VIDEO,
757 .config_props = config_output,
762 AVFilter ff_avf_showwaves = {
764 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
767 .query_formats = query_formats,
768 .priv_size = sizeof(ShowWavesContext),
769 .inputs = showwaves_inputs,
770 .activate = activate,
771 .outputs = showwaves_outputs,
772 .priv_class = &showwaves_class,
775 #endif // CONFIG_SHOWWAVES_FILTER
777 #if CONFIG_SHOWWAVESPIC_FILTER
779 #define OFFSET(x) offsetof(ShowWavesContext, x)
780 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
782 static const AVOption showwavespic_options[] = {
783 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
784 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
785 { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
786 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
787 { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
788 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
789 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
790 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
791 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
792 { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
793 { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
794 { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
798 AVFILTER_DEFINE_CLASS(showwavespic);
800 static int showwavespic_config_input(AVFilterLink *inlink)
802 AVFilterContext *ctx = inlink->dst;
803 ShowWavesContext *showwaves = ctx->priv;
805 if (showwaves->single_pic) {
806 showwaves->sum = av_mallocz_array(inlink->channels, sizeof(*showwaves->sum));
808 return AVERROR(ENOMEM);
814 static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
816 AVFilterContext *ctx = inlink->dst;
817 AVFilterLink *outlink = ctx->outputs[0];
818 ShowWavesContext *showwaves = ctx->priv;
819 int16_t *p = (int16_t *)insamples->data[0];
822 if (showwaves->single_pic) {
823 struct frame_node *f;
825 ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
829 /* queue the audio frame */
830 f = av_malloc(sizeof(*f));
832 ret = AVERROR(ENOMEM);
835 f->frame = insamples;
837 if (!showwaves->last_frame) {
838 showwaves->audio_frames =
839 showwaves->last_frame = f;
841 showwaves->last_frame->next = f;
842 showwaves->last_frame = f;
844 showwaves->total_samples += insamples->nb_samples;
850 av_frame_free(&insamples);
854 static const AVFilterPad showwavespic_inputs[] = {
857 .type = AVMEDIA_TYPE_AUDIO,
858 .config_props = showwavespic_config_input,
859 .filter_frame = showwavespic_filter_frame,
864 static const AVFilterPad showwavespic_outputs[] = {
867 .type = AVMEDIA_TYPE_VIDEO,
868 .config_props = config_output,
869 .request_frame = request_frame,
874 AVFilter ff_avf_showwavespic = {
875 .name = "showwavespic",
876 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
879 .query_formats = query_formats,
880 .priv_size = sizeof(ShowWavesContext),
881 .inputs = showwavespic_inputs,
882 .outputs = showwavespic_outputs,
883 .priv_class = &showwavespic_class,
886 #endif // CONFIG_SHOWWAVESPIC_FILTER